Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript function not defined in Firefox?

I'm currently debugging the next tier of my website in Firefox and found a really weird bug in my JavaScript. Is it true, in Firefox, that functions need to be defined above any lines of code referencing those functions? That seems really strange to me.

Here's a simplified version of what was causing bugs:

var myClass = new MyClass(myCallback);

function myCallback() {
    // code
}

It threw the following bug: Error: myCallback is not defined

And here's what i needed to do for it to work in Firefox:

var myCallback = function() {
    // code
}

var myClass = new MyClass(myCallback);

So my question is: Is this normal behavior or was something else going on? I mean, do I need to take this into consideration when coding in the future?

like image 971
Ahrengot Avatar asked Jan 10 '12 20:01

Ahrengot


Video Answer


1 Answers

You must define the function before calling it when you initiate with the format:

myCallback = function() {
    // code
}

But it should be ok to define anywhere when you initiate with the format:

function myCallback() {
    // code
}
like image 104
Billy Moon Avatar answered Oct 05 '22 09:10

Billy Moon