Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #23456 > unrolled thread
| Started by | Walter Milner <w.w.milner@gmail.com> |
|---|---|
| First post | 2014-03-08 08:43 -0800 |
| Last post | 2014-03-09 11:42 +0000 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.javascript
Relationship between .prototype and [[prototype]] Walter Milner <w.w.milner@gmail.com> - 2014-03-08 08:43 -0800
Re: Relationship between .prototype and [[prototype]] Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-08 20:24 +0100
Re: Relationship between .prototype and [[prototype]] John Harris <niam@jghnorth.org.uk.invalid> - 2014-03-09 11:33 +0000
Re: Relationship between .prototype and [[prototype]] John Harris <niam@jghnorth.org.uk.invalid> - 2014-03-09 11:42 +0000
| From | Walter Milner <w.w.milner@gmail.com> |
|---|---|
| Date | 2014-03-08 08:43 -0800 |
| Subject | Relationship between .prototype and [[prototype]] |
| Message-ID | <12769af9-a7a9-4f26-826f-5d4a62de4466@googlegroups.com> |
I think that when you define a function func, an object called func.prototype is created, and func's prototype property points to it. But also func has an internal [[prototype]] property which links to Function.prototype. Why? What is the idea behind this process?
[toc] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2014-03-08 20:24 +0100 |
| Message-ID | <1984124.lNXhnOP79H@PointedEars.de> |
| In reply to | #23456 |
Walter Milner wrote: > I think that when you define a function func, an object called > func.prototype is created, and func's prototype property points to it. Incorrect. Objects have identity, not name (until they are given a naming property). Functions are objects that have a “prototype” property (created upon creation of the Function instance). The initial value of that property is a reference to an Object instance. > But also func has an internal [[prototype]] property which links to > Function.prototype. Function instances have an internal [[Prototype]] property whose value is a reference to the same object as referred to by the initial value of the “Function.prototype” property. <http://ecma-international.org/ecma-262/5.1/#sec-13.2> > Why? What is the idea behind this process? WLOG, every ECMASCript object has a prototype chain which is identified by the object's internal [[Prototype]] property. But as that property is *internal*, it is not as such available to user code, by contrast to the the “Function.prototype” property whose initial value refers to the same object. So by adding properties to the object referred to by “Function.prototype” aso., you can have all Function instances inherit new properties, especially new methods. There are several applications for this; see e.g. <http://PointedEars.de/wsvn/JSX/trunk/object.js> for examples. Different to that, you can add properties to the object referred by the “prototype” property of Function instances; only objects having this object in their prototype chain (in general, objects created by calling this function as constructor) inherit those properties. -- PointedEars FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/> Twitter: @PointedEars2 Please do not Cc: me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | John Harris <niam@jghnorth.org.uk.invalid> |
|---|---|
| Date | 2014-03-09 11:33 +0000 |
| Message-ID | <mbkoh99alq603v9476r8mkkasdc74pi95s@4ax.com> |
| In reply to | #23456 |
On Sat, 8 Mar 2014 08:43:39 -0800 (PST), Walter Milner
<w.w.milner@gmail.com> wrote:
>I think that when you define a function func, an object
>called func.prototype is created, and func's prototype
>property points to it. But also func has an internal
>[[prototype]] property which links to Function.prototype.
>
>Why? What is the idea behind this process?
Look at it this way : A function object, which is what you create when
you define a function, needs two prototype chains.
One chain is its own prototype chain, shared with other function
objects. It needs this to do some of the things you expect a function
object to do. (Things common to all objects as it happens). This chain
is carefully protected from abuse by idiot programmers. This is why
the standard talks of the property name [[prototype]] rather than a
name you can use.
The other prototype chain is just in case you want to use the function
as a constructor, as in
var a = new func("Hello world");
If you don't, it's redundant. If you do then it's the prototype chain
given to any new objects that func constructs.
When you define a function this prototype chain is a default one that
can be left unchanged if you're not bothered about the new objects'
prototypes. If you are bothered, then you can assign an object, along
with its chain in turn, to be this prototype chain in the function
object using the property name 'prototype'.
John
[toc] | [prev] | [next] | [standalone]
| From | John Harris <niam@jghnorth.org.uk.invalid> |
|---|---|
| Date | 2014-03-09 11:42 +0000 |
| Message-ID | <2tkoh9ptq7qipru9fkqa4ekkfjermou5hj@4ax.com> |
| In reply to | #23456 |
On Sat, 08 Mar 2014 20:11:15 +0100, Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote: >Walter Milner wrote: > >> I think that when you define a function func, an object called >> func.prototype is created, and func's prototype property points to it. > >Incorrect. Objects have identity, not name (until they are given a naming >property). Functions are objects that have a “prototype” property (created >upon creation of the Function instance). The initial value of that property >is a reference to another Function instance, “the standard built-in Function >prototype object”. <snip> You presumably tried to cancel this nonsense article, but be aware that news.eternal-september.org did not cancel it. You should assume that many other news servers are the same, even well-run ones. John
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web