Subj : Re: "named" vs. anonymous function To : Brendan Eich From : Brendan Eich Date : Fri Dec 10 2004 02:26 pm Brendan Eich wrote: > This is so that each function has the proper scope parent, yet we don't > have to compile each time to get a new function for each dynamic scope. > Each dynamic scope, when activated, gets a clone of the > compiler-created function object, whose __proto__ is that > compiler-created funobj. > > ECMA does not specify __proto__, so there's no way for a script to > notice this difference. It could notice that function object identity > (== or ===) differs, but that's allowed by Edition 3 (search for "joined > function object"). > > /be > >> spidermonkey shell: >> >> js> x=function foo(){} >> function foo() { >> } >> >> js> y=function (){} >> function () { >> } To clarify further, a named function object, per ECMA-262 Edition 3 11.2.5 and 13, the production: FunctionExpression : function Identifier ( FormalParameterListopt ) { FunctionBody } A named function expression is scoped by an Object created to hold its name only while it is active. That is its activation's scope parent, so that cannot match the compiler-visible static scope parent (the global object in your js shell example). Hence the clone. Named and anonymous function objects do have different scope chains, per ECMA-262 Edition 3, but that does not map directly onto the __proto__ difference you discovered. The latter is really an implementation detail allowed by the spec. /be >> >> js> x.__proto__===Function.prototype >> false >> js> x.__proto__.__proto__===Function.prototype >> true >> js> y.__proto__===Function.prototype >> true >> >> a graph perhaps better visualizing it: >> (13k) >> >> rhino does not do this: >> >> js> x=function foo(){} >> function foo() { >> } >> >> js> y=function (){} >> function () { >> } >> >> js> x.__proto__===Function.prototype >> true >> js> y.__proto__===Function.prototype >> true >> >> >> Jens .