0

I'm a newbie to javascript. I'm using firebug to debug my program and I got a "function is not defined" error. I searched online, and people said this is because of syntax error in the function. But that function is very big and I can't use firebug now (because it only gives me "not defined" error), is there any good way to debug it? Any tools to use? Thanks!

0

2 Answers 2

1

It can be caused by a syntax error in the script defining the function resulting in the script not executing and the function not being defined, but I find that usually that error occurs when you've got a typo.
For example:

function doSomething() {
}
dosomething();

There, I forgot to capitalize the s. Since JavaScript is case-sensitive, it can't find that function.


Something you could do to debug this is find which function you're actually calling, and then compare that with the list of functions that are defined. If we're only dealing with global functions, then you could paste this into a JavaScript console (with newlines removed if necessary) to see all of the global functions you've defined:

for(var key in window) {
    if(Object.prototype.hasOwnProperty.call(window, key)) {
        if(typeof window[key] === 'function' &&
           window[key].toString() !== '[object Function]' &&
           window[key].toString().indexOf('[native code]') === -1) {
            console.log(key);
        }
    }
}

(This fails to list a few in some cases (e.g. [native code] appears in a non-native function), but they're unlikely.)

Sign up to request clarification or add additional context in comments.

3 Comments

Hi icktoofay, thanks for your reply. But I double checked my code, I'm pretty sure the function names are correct. I guess the error is because of some code inside the function. But is there any easy way to debug it since the debugger is not useful now(it only says "function is not defined")?
@Fey: Well first you'll want to find where the error is. I don't know if Firebug is telling you that or not (see x711Li's answer to see how Firebug would show it to you), but if it isn't, you could try interspersing console.log calls around the places where you think it's happening. When the error occurs, the calls after it won't run, so that's a coarse way to figure out where the error is.
@Fey: Once you know where the error is, you can try adding a debugger; statement right before where the error takes place as Mohit suggested. Then you can go into the Script tab in Firebug and it should break there, letting you inspect the environment at that point.
0

You could post the function declaration. It may be that you are using a function expression

Function expression

var f = function({alert(1);});

function statement

function foo() {alert(2);}

With a function expression the function doesn't exist until that line of code is EXECUTED.

With a function statement the function exists at the time of compile.

Of course, it could also be a scoping issue, but we can't see without code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.