Clicksco / Front-End

Organisation Front End Documentation & Tooling
http://docs.clicksco.com/frontend
2 stars 1 forks source link

If you require to call a function before it is assigned, you MUST use a function declaration #65

Closed BenjaminRCooper closed 10 years ago

BenjaminRCooper commented 10 years ago

Because of how hoisting works, using a function expression will return undefined if you try to reference it before it is assigned and error if you try to call it before it is assigned.

Because function declarations are loaded before any code is executed, calling or referencing one will not return undefined or an error.

Bad Example

console.log(someFunc) // undefined
someFunc(); // TypeError - someFunc is not a function

function someFunc() {
     console.log("some text");
}

Good Example

someFunc(); // some text

var someFunc = function() {
    console.log("some text");
}
timgale commented 10 years ago

+1

BenjaminRCooper commented 10 years ago

Implemented