Talk:JavaScript/Constructors and prototypes

The following has been corrected, as it is debunked by a simple example.
 * Changing the prototype of an object doesn't automatically update the instances of the object, and as such, you will have to add the gender field manually to the already created instances.

The debunking example Pmw57 (talk) 07:11, 6 March 2009 (UTC)

The above "debunking" example is deceptive. The statement  doesn't make a difference, you'll get an unspecified alert message with or without it. Changing 'unspecified' to something else will show you that the original example is in fact correct. Seipher (talk) 20:36, 8 October 2010 (UTC)


 * Of course it makes a difference. Remove the statement and run the example, then you won't get the unspecified message. Did you try it? and what original example are you talking about? The example was added after that comment. 81.187.215.34 (discuss) 15:50, 2 July 2016 (UTC)

Ambiguity of "changing the prototype":-

If the whole object is replaced then this will not be seen by the objects already created. It will be seen by newly created objects.

Because Animal.prototype = anewprototype;

will replace the prototype, objects that already exist will keep the reference to the old prototype. Newly created objects will get the new prototype.

Whereas Animal.prototype.gender = anewvalue;

does not replace the prototype, it modifies the object that is the referenced by all the objects already created. Newly created objects will get the same prototype.

QuentinUK (discuss • contribs) 00:22, 23 February 2013 (UTC)

Prototypes
"A prototype for an object is the set of auto-created fields and methods. It cannot operate by itself, and relies on an existing constructor."

Prototypes don't need any constructor. See Object.create function for example.

"When the object is created, the fields initialized in the prototype are copied to the newly created object."

This is wrong, the fields aren't copied. Instead the whole prototype object from the constructor is referenced from the new object. 81.187.215.34 (discuss) 16:05, 2 July 2016 (UTC)