Prev: FAQ Topic - How do I POST a form to a new window? (2010-02-27)
Next: JSLint Reports on last 2 Production Versions of jQuery
From: Garrett Smith on 27 Feb 2010 02:59 The subject of functions comes up enough that it warrants a section in the FAQ. The section could be inserted before dates. Of the questions on functions, the most common is that of anonymous functions used to create closures, so as 4 Functions 4.1 What is (function(){ /*...*/ })() ? 5 Dates 5.1 How do I... Functions are often used to create scope. By creating an anonymous function, the variables in that function are not accessible from outside the function. This is desirable for example, to hide implementation details or avoid polluting the global scope. For more information, please see: yura.thinkweb2.com/named-function-expressions/ http://www.jibbering.com/faq/notes/closures/ I would also like to fix an error in the closures article from: | The instance of MyObject2 referred to by the objectRef variable has a | prototype chain. The first object in that chain is the instance of | MyObject1 that was created and assigned to the prototype property of | the MyObject2 constructor. The instance of MyObject1 has a prototype, | the default Object prototype that corresponds with the object referred | to by Object.prototype. Object.prototype has a null prototype so the | prototype chain comes to an end at this point. to: | The instance of MyObject2 referred to by the objectRef variable has a | prototype chain. The first object in that chain is the instance of | MyObject1 that was created and assigned to the prototype property of | the MyObject2 constructor. The next object in that chain is the | [[Prototype]] of the MyObject1 instance; it is MyObject1.prototype. | The next object in the chain is the [[Prototype]] of | MyObject1.prototype, which is Object.prototype. Object.prototype has a | null [[Prototype]] so the prototype chain comes to an end at this | point. | | Prototype Diagram | objectRef [[Prototype]] -> MyObject1 [[Prototype]] -> | MyObject1.prototype [[Prototype]] -> Object.prototype. I also propose the removal that the final paragraph of that section (below). The article already discusses [[Prototype]], but uses the term "prototype" to describe it. I prefer to use the spec's [[Prototype]] notation to avoid confusion between a Function's prototype property and Objects' internal [[Prototype]] property. Propose to remove: | Note: ECMAScript defines an internal [[prototype]] property of the | internal Object type. This property is not directly accessible with | scripts, but it is the chain of objects referred to with the internal | [[prototype]] property that is used in property accessor resolution; | the object's prototype chain. A public prototype property exists to | allow the assignment, definition and manipulation of prototypes in | association with the internal [[prototype]] property. The details of | the relationship between to two are described in ECMA 262 (3rd | edition) and are beyond the scope of this discussion. -- Garrett comp.lang.javascript FAQ: http://jibbering.com/faq/
From: Garrett Smith on 28 Feb 2010 00:38 Garrett Smith wrote: > The subject of functions comes up enough that it warrants a section in > the FAQ. The section could be inserted before dates. > [...] > | Prototype Diagram > | objectRef [[Prototype]] -> MyObject1 [[Prototype]] -> > | MyObject1.prototype [[Prototype]] -> Object.prototype. > | objectRef [[Prototype]] --> MyObject2.prototype (MyObject1 instance) | [[Prototype]] --> MyObject1.prototype [[Prototype]] --> | Object.prototype. Thoughts or comments? -- Garrett comp.lang.javascript FAQ: http://jibbering.com/faq/
From: Asen Bozhilov on 28 Feb 2010 03:01 Garrett Smith wrote: > | Prototype Diagram > | objectRef [[Prototype]] -> MyObject1 [[Prototype]] -> > | MyObject1.prototype [[Prototype]] -> Object.prototype. The diagram is correct, but a little bit confused me. 1. `objectRef` refer `object', which has internal [[Prototype]] property, which refer `MyObject2.prototype`. 2. `object' referred from `MyObject2.prototype` has [[Prototype]] property, which refer `MyObject1.prototype`. 3. `MyObject1.prototype' refer to `Object.prototype` from [[Prototype]] property. 4. `Object.prototype` has primitive value `null' for [[Prototype]] property. And from these steps i can build the next diagram: objectRef -> MyObject2.prototype -> MyObject1.prototype -> Object.prototype -> null I thing, FAQ of c.l.js must have separate article for Prototype chain. You can write that article, with suggestions from other members of c.l.js. Regards.
From: Garrett Smith on 28 Feb 2010 03:49 Asen Bozhilov wrote: > Garrett Smith wrote: > >> | Prototype Diagram >> | objectRef [[Prototype]] -> MyObject1 [[Prototype]] -> >> | MyObject1.prototype [[Prototype]] -> Object.prototype. > > The diagram is correct, but a little bit confused me. > > 1. `objectRef` refer `object', which has internal [[Prototype]] > property, which refer `MyObject2.prototype`. > 2. `object' referred from `MyObject2.prototype` has [[Prototype]] > property, which refer `MyObject1.prototype`. > 3. `MyObject1.prototype' refer to `Object.prototype` from > [[Prototype]] property. > 4. `Object.prototype` has primitive value `null' for [[Prototype]] > property. > I like that better than the paragraph. It breaks up the article better. > And from these steps i can build the next diagram: > > objectRef -> MyObject2.prototype -> MyObject1.prototype -> > Object.prototype -> null > > I thing, FAQ of c.l.js must have separate article for Prototype chain. > You can write that article, with suggestions from other members of > c.l.js. > I too felt the discussion of [[Prototype]] chain was a bit out of place in that article. My intent was to preserve the article, move it to a /faq/notes/, as has been discussed, and in the process, fix an error and add some navigation. Perhaps at a later date, an article on prototype chain could be linked to, obviating the need for that. I can write or, you can write it :-D. Maybe you can enlist some help like kangax or Dmitry or David Mark. -- Garrett comp.lang.javascript FAQ: http://jibbering.com/faq/
From: abozhilov on 28 Feb 2010 07:35
Garrett Smith wrote: > I too felt the discussion of [[Prototype]] chain was a bit out of place > in that article. My intent was to preserve the article, move it to a > /faq/notes/, as has been discussed, and in the process, fix an error and > add some navigation. Yes, but in the other hand. If people don't know how property resolve against Prototype chain, they cannot understand, how identifier resolve against Scope Chain. Scope Chain contain Variable Objects. ECMA 262 standard, doesn't specify implementation of AO/VO. From that point, AO/VO can have [[Prototype]] property, which refer next `object' in Prototype chain. And during Identifier Resolution algorithm specified in `10.1.4 Scope Chain and Identifier Resolution` will check for existing in Prototype chain of next `object' in Scope Chain, for property with name as `Identifier` from step 2: | 2. Call the [[HasProperty]] method of Result(1), | passing the Identifier as the property. And from `8.6.2.4 [[HasProperty]] (P)`. > I can write or, you can write it :-D. Maybe you can enlist some help > like kangax or Dmitry or David Mark. Yes. If you have time and desire for writing this article. We can help you. For example Dmitry has article for Prototype inheritance in ECMA-262 implementations on Russian. <URL: http://dmitrysoshnikov.com/ecmascript/ru-chapter-7-1-oop-general-theory/ > <URL: http://dmitrysoshnikov.com/ecmascript/ru-chapter-7-2-oop-ecmascript-implementation/ > And of course, in archive of newsgroup have brilliant explanations from Thomas and Richard. I thing, we can create very good article, which is useful for people whose want to know how Prototype inheritance work in ECMA-262 implementations. Regards. |