Subj : Re: "named" vs. anonymous function To : Jens Thiele From : Brendan Eich Date : Mon Dec 13 2004 10:02 am Jens Thiele wrote: > Brendan Eich schrieb: > >>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 > > > So this this is not really a matter of named vs. anonymous function but > a matter of scope? The spec mandates a difference in scope between named and unnamed functions: an Object instance needed to hold the name must scope an activation of the named function. > Test: > js> (function(){return function(){};})().__proto__===Function.prototype > false > js> (function(){return > function(){};})().__proto__.__proto__===Function.prototype > true > > though this example strictly wouldn't require a scope object? > > perhaps better? > js> (function (){var i=10;return function(){return > i;};})().__proto__===Function.prototype > false > js> (function (){var i=10;return function(){return > i;};})().__proto__.__proto__===Function.prototype > true > > looking at the source (jsinterp.c) > the relevant parts seem to be: > JSOP_ANONFUNOBJ, JSOP_NAMEDFUNOBJ and JSOP_CLOSURE > (probably not JSOP_DEFFUN) > > in any way it is the call to js_CloneFunctionObject > creating the clone / additional prototype > > I think I better stop now and get back to work again. Curiosity killed > the cat ;-) As you've observed, independent of the named vs. anonymous scope (parent-linked chain) difference mandated by ECMA, SpiderMonkey uses a proto-linked chain difference to share precompiled function objects among clones and the compiler-created "clone parent." Don't mix up these cases, or the sense of "parent" used by the engine and ECMA with the "clone parent" sense I just introduced! ;-) /be .