2

I understand prototypes can be used to extend properties to their children. However I don't understand why you would want to have a "Object.prototype.property" in the main object constructor class itself?

In the Person constructor why Person.prototype.name = "Nicholas"; VS this.name = "Nicholas";

 function Person(){

 Person.prototype.name = "Nicholas";
 Person.prototype.age = 29;
 Person.prototype.job = "Software Engineer";
 };

 var person1 = new Person();
 var person2 = new Person();

 person1.name = "Greg";
 alert(person1.name);   //"Greg" – from instance
 alert(person2.name);   //"Nicholas" – from prototype
  • 2
    It's hard to tell what's going on in your code; is there a stray `}` in there? Setting prototype properties in a constructor is rarely a good idea. – Pointy Aug 07 '14 at 18:20
  • 1
    You don't often see a constructor setting up its own prototype. It's usually done outside the constructor. – guest Aug 07 '14 at 18:34
  • Take the time to make sure what you type is correct. For starter, look at the braces, for every opening brace { there should a matching closing brace } and no more. – Kevin Le - Khnle Aug 07 '14 at 20:47
  • Set data to object prototype is wrong architecture decision. Prototype are shared between all instances – and to save memory is the best place to store methods, not data. Maybe you have to much simplify example? – Krzysztof Safjanowski Aug 07 '14 at 21:00
  • Setting prototype in the constructor function doesn't make much sense. Why that is so is explained in detail here. http://stackoverflow.com/questions/16063394/prototypical-inheritance-writing-up/16063711#16063711 – HMR Aug 08 '14 at 01:00
  • See also [Assigning prototype methods *inside* the constructor function - why not?](http://stackoverflow.com/q/28255957/1048572) – Bergi Jan 31 '15 at 20:41

2 Answers2

1

Person.prototype.name = 'Nicholas' and this.name = 'Nicholas' are NOT the same.

Let's see for ourselves:

var PersonA = function () {
    this.name = 'Nicholas';
    this.species = 'Homo Sapiens'
    return this; // optional
};
var PersonB = function () {
    return this;
};
PersonB.prototype.name = 'Nicholas';
PersonB.prototype.species = 'Homo Sapiens'

var personA = new PersonA();
console.log(Object.keys(personA)); // logs ["name", "species"]

var personB = new PersonB();
console.log(Object.keys(personB)); // logs []

console.log(personA.species === personB.species); // logs true

Clearly, personA has a 2 keys, "name" and "species"; while personB no keys!.

However, the prototype of personB 2 keys, "name" and "species"

Now, if you create a million objects of PersonA, each such object would have a
"name": "Nicholas" and "species": "Homo Sapiens" key-value pairs.

On the other hand, creating a million objects of PersonB would be much more memory-efficient as each such object would have no key-value pairs at all! At the same time, both "name" and "species" will be available through prototype (prototypal inheritance).

A closer look at refinements:

Lets examine the refinement on an object obj, say obj.foo:

  • If "foo" is a property of obj, the corresponding value is returned.
  • Otherwise, the prototype of obj is looked at.
  • If that prototype has a property "foo", the corresponding value is returned.
  • Otherwise that prototype's prototype is looked at.
  • The process continues till we hit Object.prototype.

So in terms of memory, PersonB is superior to PersonA. This is the beauty of prototypal inheritance.

Hope this helps.

Sumukh Barve
  • 1,414
  • 15
  • 12
1

Setting a prototype property in the constructor is almost certainly an error. The prototype is the shared state of all child objects. The only reason you might want to update the prototype in the constructor is if you are storing shared data. For example, you might increment a counter, or add to a sum. But you would almost never expect for a simple string property like "name" to be set on the prototype within the constructor.

Rich Remer
  • 2,123
  • 1
  • 21
  • 22