bar // s...">

Call javascript function from Chrome console

Very simple script:

function foo(){ return "bar" } console.log( foo() ); 

Prefixes:

 > bar // sanity test that proves everything working > foo(); // this guy is key! > ReferenceError: foo is not defined 

How do I call foo (); for debugging and experimenting purposes?

Is this not a practice? I like to use the IRB / Rails Console to test my ruby ​​code and want to do the same with JavaScript

http://jsfiddle.net/m2muL/

+8
javascript
source share
2 answers

The problem is that your foo function is not part of the global scope. The console has access to everything that window does. As a result, if it is undefined, then it is undefined in the console. For example, this might be an example of how foo is not available in the console.

 (function(){ function foo(){ return "bar"; } console.log(foo()); //"bar" })() console.log(foo()); //ReferenceError: foo is not defined 

Find a way to locate this method. If it is inside an object or method, be sure to access it from the console.

 var foobar = { foo: function(){ return "bar" ;} }; console.log(foobar.foo()); //"bar" 

If you cannot reference foo, you cannot invoke it.

+8
source share

You are trying to do this in a JSFiddle that "hides" your javascript from your console. This is not real for you. He does not work there, as if you assume that he ...

If you created a simple HTML file and pinned your javascript there, between the tags, you would not have a problem running "foo ()" in the console.

Create test.html and put this inside:

 <script> function foo(){ return "bar" } console.log( foo() ); </script> 
+2
source share

All Articles