Friday, 15 July 2011

What is the difference between a function expression vs declaration in JavaScript? -



What is the difference between a function expression vs declaration in JavaScript? -

this question has reply here:

var functionname = function() {} vs function functionname() {} 25 answers

what difference between next lines of code?

//function declaration function foo() { homecoming 5; } //anonymous function look var foo = function() { homecoming 5; } //named function look var foo = function foo() { homecoming 5; } what named/anonymous function expression? what declared function? how browsers deal these constructs differently?

what responses similar question (var functionname = function() {} vs function functionname() {}) not right?

they're similar. how phone call them same, difference lies in how browser loads them execution context.

function declarations loads before code executed.

while function expressions loads when interpreter reaches line of code.

so if seek phone call function look before it's loaded, you'll error

but if phone call function declaration, it'll work. because no code can called until declarations loaded.

ex. function expression

alert(foo()); // error! foo wasn't loaded yet var foo = function() { homecoming 5; }

ex. function declaration

alert(foo()); // alerts 5. declarations loaded before code can run. function foo() { homecoming 5; }

as sec part of questions.

var foo = function foo() { homecoming 5; } same other two. it's line of code used cause error in safari, though no longer does.

javascript

No comments:

Post a Comment