Tuesday 15 June 2010

javascript - Defining functions after return -



javascript - Defining functions after return -

i'm reading john papa's angularjs style guide , saw the code:

class="lang-js prettyprint-override">function dataservice() { var somevalue = ''; var service = { save: save, somevalue: somevalue, validate: validate }; homecoming service; //////////// function save() { /* */ }; function validate() { /* */ }; }

you can see functions save , validate defined after function returned value. how work? standard-compliant , works in browsers (say, ie 6)?

you can see functions save , validate defined after function returned value.

that's looks they're written, yes, in fact they're defined before step-by-step code in function runs @ all. called "hoisting" declarations top of function (something similar happens var, too; more below).

when command enters execution context (e.g., when come in function, come in global environment @ origin of program, or come in eval code), 1 of several things happens before step-by-step code executed of function declarations in context processed , functions created. since save , validate defined function declarations, they're created before first step-by-step line of code runs, , doesn't matter they're after return.

here's javascript engine when phone call function (e.g., when calling dataservice), function declarations step highlighted:

set value of this create new environment (let's phone call env) call set reference function’s [[scope]] property on env (this part of how closures work) create binding object (let's phone call bindings) environment hold our various names defined function (this part of how closures work, , how variable references resolved) if function has name, add together bindings property referring function add formal function arguments bindings process function declarations, adding names bindings create arguments object, add together bindings add each variable declared var bindings (if not defined) value undefined process stepwise code in function set phone call expression’s result

this laid out in excruciating detail in spec in §10.4.1 , sections links to. (if go read that, brace yourself, prose is...turgid...) that's link current spec, laid out in §10 of old 3rd edition spec in 1999 well, , i'm sure it's been true right beginning.

is standard-compliant , works in browsers (say, ie 6)?

yes. used create me nervous, several years (probably ~2005) proved myself on of then-current , not-quite-dead browsers find (including ie6), , universally handled correctly. isn't surprising, because it's makes code work:

dosomething(); function dosomething() { // .... }

...and people all time.

this "hoisting" 1 of key differences between function declarations , function expressions. if save , validate created function expressions, matter great deal written after return — they'd never created @ all:

// wouldn't work this, instance function dataservice() { var somevalue = ''; var service = { save: save, // `save` has value `undefined` @ point somevalue: somevalue, validate: validate // `validate` }; homecoming service; //////////// var save = function() { // function look /* */ }; var validate = function() { // /* */ }; }

the save , validate variables created (thanks step 9 in above), of they're used, they'd have value undefined , returned object wouldn't useful.

javascript angularjs closures standards-compliance

No comments:

Post a Comment