|
View:
New views
3 Messages
—
Rating Filter:
Alert me
|
|
|
question about creating function objectsIn ECMA-262 specification page 72, section 13.2 Creating Function
Objects, it says Given an optional parameter list specified by FormalParameterList, a body specified by FunctionBody, and a scope chain specified by Scope, a Function object is constructed as follows: 1. If there already exists an object E that was created by an earlier call to this section's algorithm, and if that call to this section's algorithm was given a FunctionBody that is equated to the FunctionBody given now, then go to step 13. (If there is more than one object E satisfying these criteria, choose one at the implementation's discretion.) 2. Create a new native ECMAScript object and let F be that object. 3. Set the [[Class]] property of F to "Function". 4. Set the [[Prototype]] property of F to the original Function prototype object as specified in 15.3.3.1. 5. Set the [[Call]] property of F as described in 13.2.1. 6. Set the [[Construct]] property of F as described in 13.2.2. 7. Set the [[Scope]] property of F to a new scope chain (10.1.4) that contains the same objects as Scope. 8. Set the length property of F to the number of formal properties specified in FormalParameterList. If no parameters are specified, set the length property of F to 0. This property is given attributes as specified in 15.3.5.1. 9. Create a new object as would be constructed by the expression new Object(). 10. Set the constructor property of Result(9) to F. This property is given attributes { DontEnum }. 11. Set the prototype property of F to Result(9). This property is given attributes as specified in 15.3.5.2. 12. Return F. 13. At the implementation's discretion, go to either step 2 or step 14. 14. Create a new native ECMAScript object joined to E and let F be that object. Copy all non-internal properties and their attributes from E to F so that all non-internal properties are identical in E and F. 15. Set the [[Class]] property of F to "Function". 16. Set the [[Prototype]] property of F to the original Function prototype object as specified in 15.3.3.1. 17. Set the [[Call]] property of F as described in 13.2.1. 18. Set the [[Construct]] property of F as described in 13.2.2. 19. Set the [[Scope]] property of F to a new scope chain (10.1.4) that contains the same objects as Scope. 20. Return F. NOTE A prototype property is automatically created for every function, to allow for the possibility that the function will be used as a constructor. Step 1 allows an implementation to optimise the common case of a function A that has a nested function B where B is not dependent on A. In this case the implementation is allowed to reuse the same object for B instead of creating a new one every time A is called. Step 13 makes this optimisation optional; an implementation that chooses not to implement it will go to step 2. For example, in the code function A() { function B(x) {return x*x;} return B; } function C() { return eval("(function (x) {return x*x;})"); } var b1 = A(); var b2 = A(); function b3(x) {return x*x;} function b4(x) {return x*x;} var b5 = C(); var b6 = C(); an implementation is allowed, but not required, to join b1 and b2. In fact, it may make b1 and b2 the same object because there is no way to detect the difference between their [[Scope]] properties. On the other hand, an implementation must not join b3 and b4 because their source codes are not equated (13.1.1). Also, an implementation must not join b5 and b6 because they were produced by two different calls to eval and therefore their source codes are not equated. In practice it's likely to be productive to join two Function objects only in the cases where an implementation can prove that the differences between their [[Scope]] properties are not observable, so one object can be reused. By following this policy, an implementation will only encounter the vacuous case of an object being joined with itself. My question concerns step 4 and step 11. At step 11, the *prototype* property of F is set to Result(9). I am wondering what is the difference between the *prototype* property and the [[Prototype]] property of F as appearing at step 4. Another related question is why it needs to create a new object as would be by the expression new Object() at step 9? Thanks, Yan _______________________________________________ dev-tech-js-engine mailing list dev-tech-js-engine@... https://lists.mozilla.org/listinfo/dev-tech-js-engine |
|
|
Re: question about creating function objectsOn Thu, Nov 5, 2009 at 6:25 PM, Yan Huang <yh8h@...> wrote:
> My question concerns step 4 and step 11. At step 11, the *prototype* > property of F is set to Result(9). I am wondering what is the > difference between the *prototype* property and the [[Prototype]] > property of F as appearing at step 4. Step 4 describes what is in Spidermonkey the "__proto__" property. We can confirm this introspectively: js> function f(){} js> f.__proto__ === Function.prototype true Step 11 describes the "prototype" property. Not a confirmation, but the Javascript language equivalent of step 11: js> f.prototype = new Object > Another related question is why it needs to create a new object as > would be by the expression new Object() at step 9? The "prototype" property of a function F becomes the prototype (again, known as the "__proto__" property in Spidermonkey) of an object that is instantiated by invoking the function F as a constructor with the "new" operator. We can also confirm this introspectively: js> g = new f [object Object] js> g.__proto__ === f.prototype true -- http://codebad.com/ _______________________________________________ dev-tech-js-engine mailing list dev-tech-js-engine@... https://lists.mozilla.org/listinfo/dev-tech-js-engine |
|
|
Re: question about creating function objectsOn Thu, Nov 5, 2009 at 6:25 PM, Yan Huang <yh8h@...> wrote:
> My question concerns step 4 and step 11. At step 11, the *prototype* > property of F is set to Result(9). I am wondering what is the > difference between the *prototype* property and the [[Prototype]] > property of F as appearing at step 4. Step 4 describes what is in Spidermonkey the "__proto__" property. We can confirm this introspectively: js> function f(){} js> f.__proto__ === Function.prototype true Step 11 describes the "prototype" property. Not a confirmation, but the Javascript language equivalent of step 11: js> f.prototype = new Object > Another related question is why it needs to create a new object as > would be by the expression new Object() at step 9? The "prototype" property of a function F becomes the prototype (again, known as the "__proto__" property in Spidermonkey) of an object that is instantiated by invoking the function F as a constructor with the "new" operator. We can also confirm this introspectively: js> g = new f [object Object] js> g.__proto__ === f.prototype true -- http://codebad.com/ _______________________________________________ dev-tech-js-engine mailing list dev-tech-js-engine@... https://lists.mozilla.org/listinfo/dev-tech-js-engine |
| Free embeddable forum powered by Nabble | Forum Help |