![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
|
differ in their Scope chains, and when you create them inside the same function body during one call to that function, then the scope chains will also be identical. Perhaps theoretically possible, but I find no evidence that either IE6 or Moz 1.3 "join" function objects as described in ECMA 262, and in practice a new function object is created for every execution of an inner function definition of the form |
#2
| |||
| |||
|
|
Joining of functions (which I hadn't read in detail before) is actually interesting. Compare --- var x=[]; for (var i=0;i<2;i++){ function foo(){alert(i);}; x[i]=foo; } x[0]==x[1] --- and --- var x=[]; for (var i=0;i<2;i++){ var foo = function(){alert(i);}; x[i]=foo; } x[0]==x[1] --- The two does act differently in some browsers (meaning that var foo = function(){} and function foo(){} are not interchangable in all cases). In Netscape 4, both give false. In Mozilla, both give true. In IE6 and Opera 5+, the first gives true and the second false. |
|
All are legal, since it is at the implementation's discretion whether to create joined objects or not. |
#3
| |||||
| |||||
|
|
I was interested in your results from these tests but I was unable to reproduce them, particularly your results for Gecko browsers. |
|
The following are my test functions and results:- function funcTest1(){ var x=[]; for (var i=0;i<2;i++){ function foo(){alert(i);}; x[i] = foo; } return (x[0]==x[1]); } An inner function declaration within the - for - loop. Mozilla 1.0, 1.2, 1.3, Netscape 7.02 and K-Meleon 0.7 (Gecko browsers) == false Opera 6.05 & 7.11and IE 6 == true Netscape 4.79 == false |
|
There seems to be a discrepancy in the interpretation of the first function. The inner function declaration looks like it might be being interpreted as a function expression with optional identifier by Net 4 and Gecko browsers, but even then the results do not conform with my interpretation of ECMA section 10.1.3 and 13. |
|
I also performed an additional test to gauge the extent to which function objects would be joined or reused. Following the examples in the footnote to ECMA 262 section 13.2:- .... - I would have to conclude that none of these browsers have chosen to implement the optional optimisation mentioned in ECMA 262 section 13.2 (and 13.1.2) and that no joining of function objects is going on. |
|
Having apparently ascertained that any inner function returned by a function will be a unique function object and that function expressions produce unique function objects I think that this has implications for how inner functions should be used. .... |
#4
| |||||
| |||||
|
|
I was interested in your results from these tests but I was unable to reproduce them, particularly your results for Gecko browsers. It seems the context is important too. I evaluated the expressions using eval (in my test page: URL:http://www.infimum.dk/HTML/javascript/jstest4.html>) That means that the code was part of an eval block, not a function body. |
|
... The inner function declaration looks like it might be being interpreted as a function expression with optional identifier by Net 4 and Gecko browsers, but even then the results do not conform with my interpretation of ECMA section 10.1.3 and 13. Indeed, it couldn't be a function expression with identifier, since that would not declare a "foo" variable in the surroundings, and "foo" has been declared in the next line. |
|
I do think this is in accordance with ECMA262 section 13.2, and what we are seeing from IE and Opera *is* the joining of the functions. |
|
... Good points, if execution speed and memory are the primary goals. |
|
However, I am weary at designing programs for such a diverse and evolving platform as browsers' Javascript by aiming at the artifacts of the current impementations. I would much prefer to program for ease of maintaining, and only optimize when the need for speed is dire. |
#5
| |||
| |||
|
|
It seems the context is important too. I evaluated the expressions using eval (in my test page: URL:http://www.infimum.dk/HTML/javascript/jstest4.html>) That means that the code was part of an eval block, not a function body. I will look at that page later. Now I am also going to have to reread the ECMA Script sections on eval code and see how expected your results were in that context. snip |
#6
| |||
| |||
|
|
"Lasse Reichstein Nielsen" <lrn (AT) hotpop (DOT) com> wrote SNIP |
]
- which Mozilla did not:

#7
| |||||
| |||||
|
|
I too was unable to reproduce this result (in an evaluator as well, http:/dfxhtml.com/browserpad/pad.html ) |
|
and put it down to version differences between our Mozillas - I am running 1.3. Did you test in a later version? *Minding that* the answer if for interest. ... snip |
|
quote [Richard] function funcTest10(){ while(false){ function foo(){alert(i);}; } return foo; } alert(funcTest10()); Where Opera and IE alert the toString version of foo and Gecko browsers produce a JavaScript error - foo is undefined - on the - return foo; - line. /quote In this case IE and Opera are wrong in the return value. Yes they have done the ECMA 10.1.3 thing in initialising the activation object but failed to spot the missing paragraph margin in ECMA-262.pdf section 13 Syntax - tsk tsk - whichMozilla did not: cite NOTE The Identifier in a FunctionExpression ... snip /cite |
|
However, this is not related to whether the browsers might or might not have joined the foo function in a loop or across calls, and *IHMO* the programmer who puts a named function expression in a loop is asking for *what exactly*? What they get is my answer ![]() |
|
My thoughts and observations are that javascript engines are empirically unlikely to join (closure) function objects across multiple calls to a container function at this time (I haven't seen any), but attribute this to the interpretative nature of javascript (subject to optimisation at any time) rather than the content of ECMA 262. |
![]() |
| Thread Tools | |
| Display Modes | |
| |