Thursday, 15 April 2010

ecmascript 5 - Nested functions Javascript -



ecmascript 5 - Nested functions Javascript -

in javascript function, required define nested functions function expressions or function declarations allowed in function body? example, compliant?

function a() { function b() { function c() { window.alert(3); } window.alert(2); } window.alert(1); }

or have this?

function a() { var = function () { var c = function () { window.alert(3); } window.alert(2); } window.alert(1); }

ecma-262 says that:

several used implementations of ecmascript known back upwards utilize of functiondeclaration statement. there important , irreconcilable variations among implementations in semantics applied such functiondeclarations. because of these irreconcilable differences, utilize of functiondeclaration statement results in code not reliably portable among implementations. recommended ecmascript implementations either disallow usage of functiondeclaration or issue warning when such usage encountered. future editions of ecmascript may define alternative portable means declaring functions in statement context.

does mean function declaration in function body technically incorrect, or have got wrong? i've heard people refer body block, according standard, 1 or more statements, i'm not sure.

functiondeclaration's allowed in function body, , there no bugs i'm aware of.

it's pretty clear 1 time @ es5 production rules

functionbody sourceelements (opt) sourceelements sourceelement sourceelements sourceelement sourceelement statement functiondeclaration

in other words, function body includes source elements, , source element either statement or functiondeclaration. hence functiondeclaration can part of function body.

the clause mentioned "... implementations of ecmascript ... known back upwards utilize of functiondeclaration statement" refers using functiondeclaration statement, not straight in function body. it's referring cases like:

if (...) { function f () {} }

that non-standardized behavior. check out more on these function statements.

javascript ecmascript-5

No comments:

Post a Comment