7

On the window.onload event, I would like a function to be called. If I define it as given below, the function does not get called.

try {
    window.addEventListener("load", initialiseTable, false);
} catch(e) {
    window.onload = initialiseTable;
}

var initialiseTable = function () {
    console.log("hello world!!");
};

but if I change the function declaration to

function initialiseTable() {
    console.log("hello world!!");};

it works, any idea?

101V
  • 492
  • 1
  • 6
  • 13
  • 1
    I'd say the problem is the execution order as `initialiseTable` is used to previous to declaration – Claudio Redi Jun 20 '13 at 15:44
  • Take a look here http://stackoverflow.com/questions/336859/javascript-var-functionname-function-vs-function-functionname – Iurii.K Jun 20 '13 at 15:47
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Scope_Cheatsheet#function_oddities – Shmiddty Jun 20 '13 at 16:06
  • Late comment, but this is why you should please provide info rather than linking to it. That developer.mozilla.org link is no good anymore, no clue what it used to say. – John Smith May 02 '19 at 07:03

5 Answers5

6

While you using var x = function(){} to declare functions, it should be declare before you call the function.

By function x (){}, x will exist in current scope, no matter how later it declare. Because such side effect, the var x = function(){} are more recommended. For example:

var user = 'alien';
if( user == 'alien') {
    function salute () { console.log("Welcome to Earth!"); }
} else {
    function salute (){ console.log("Good day!"); }
}

The salute() will print Good day! anyway, even though it not we want at all.

taiansu
  • 2,735
  • 2
  • 22
  • 29
2

Declare the function before.

var initialiseTable = function () {
    console.log("hello world!!");
};
try {
    window.addEventListener("load", initialiseTable, false);
} catch(e) {
    window.onload = initialiseTable;
}

For more information about declaring a function read this: var functionName = function() {} vs function functionName() {}

Community
  • 1
  • 1
Edorka
  • 1,781
  • 12
  • 24
1

Just put the function initialiseTable to the first position, eg:

var initialiseTable = function (){ /*[...]*/ }

After continue calling.

Mystic
  • 381
  • 1
  • 13
1

Since you come from C#, the difference is a lot like:

class A {
    public int method2() {
        //I can call method1 even though it *declared* later below
        int a = method1(); 
    }

    public int method1() {

    }
}

vs

class B {
    public int method2() {
        bool a = method1(3); //I can't call it because it is *assigned* later below.
                         //In fact I cannot even compile it 
        Func<int, bool> method1 = x => x == 3;
    }
}

You may notice the difference in javascript, if you look at:

function a() {

}

There is no assignment anywhere.

Where as with var a = function(){} there is obviously assignment and it is similar to the latter C# example.

Esailija
  • 138,174
  • 23
  • 272
  • 326
0

Function declaration should always precede its call.

At this line:-

window.addEventListener("load", initialiseTable, false);

initialiseTable is not known so it should be placed after function definition. Actually function statement causes its identifier to be bound before anything in its code-block* is executed.Function declarations loads before any code is executed. It differs from the function expression which is evaluated in normal top-down order. Function expressions loads only when the interpreter reaches that line of code. so if you use:-

var initialiseTable = function () {
    console.log("hello world!!");
};

it won't work but this works:-

function initialiseTable() {
    console.log("hello world!!");};
Vivek Sadh
  • 4,230
  • 3
  • 32
  • 49