4

In the below code,

function Person(first, last, age) {
    this.firstName = first;
    this.lastName = last;
    this.age = age;
}

Person.prototype.planet = "Earth";

p1 = new Person("David", "Beckham", 39);
p2 = new Person("Lionel", "Messi", 30);

If multiple instances p1 p2 are created using constructor Person, then

How do I understand the difference about the property planet with property age? What difference it would make by adding property planet as this.planet in the constructor Person?

Note: Understanding prototype property

Krzysztof Safjanowski
  • 7,292
  • 3
  • 35
  • 47
overexchange
  • 15,768
  • 30
  • 152
  • 347

3 Answers3

4

Consider situation when in the fututre we are going to change prototype property that is shared by all instances

function Person(first, last, age) {
    this.firstName = first;
    this.lastName = last;
    this.age = age;
}

Person.prototype.planet = "Earth";

p1 = new Person("David", "Beckham", 39);
p2 = new Person("Lionel", "Messi", 30);

console.log(p1.planet) // Earth

Person.prototype.planet = "Mars"

console.log(p1.planet) // Mars
console.log(p1.planet === p2.planet) // true

Changing one property on prototype will change it in all instances

Krzysztof Safjanowski
  • 7,292
  • 3
  • 35
  • 47
3

A prototype property will be part of any object created from the so-called prototype, and this includes prototype chain.

A instance property will be part of the whole instance, and in your case, it will part of any instance because you're adding it within the constructor function:

function A() {
   this.x = 11;
}

var instance = new A();
instance.x = 11;

Both above cases are adding the property to the own object rather than in the prototype.

Furthermore, adding properties to the prototype has a side effect:

function A() {}
A.prototype.x = 11;

function B() {}
B.prototype = Object.create(A.prototype);

var instanceA = new A();
var instanceB = new B();

A.prototype.x = 12;

// Both "x" will hold 12
alert(instanceA.x);
alert(instanceB.x);

Learn more about prototype chain on MDN.

About some OP comment

So, In java terminology, age is an instance member and planet is a static member. To define a static member, we use prototype property, am I correct? –

This is a wrong statement.

Prototype properties aren't static, since prototypes are regular objects. It's just JavaScript uses prototype chain to implement inheritance and it relies in a standard property called prototype.

In JavaScript there're no statics. When you access any property, JavaScript's runtime will look for it through the prototype chain:

function A() {};
A.prototype.x = 11;

function B() {};
B.prototype = Object.create(A.prototype);

function C() {};
C.prototype = Object.create(B.prototype);

var instanceC = new C();
var x = instanceC.x;
// Once you request a property "x", the runtime will do the following process:
// 1) Is "x" in the own object? No, then 2)
// 2) Is "x" in current object's prototype? No, then 3)
// 3) Is "x" in the parent prototype? No, then 4)
// 4) And so on, until it reaches the top-level prototype, and if this has no
//    "x" property, then runtime will return "undefined"
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
  • I hardly understand this statement: *A prototype property will be part of any object created from the so-called prototype, and this includes prototype chain.* and the remaining answer is duplicate. Currently am assuming I know creating objects only through constructors. – overexchange Aug 20 '15 at 09:37
  • @overexchange This is why I linked you to MDN, to learn more about prototype chain. BTW, I've provided you an update based on some comment you added to your question, and it summarizes prototype chain... – Matías Fidemraizer Aug 20 '15 at 09:39
  • I started learning Javascript using MDN. I would strongly say that, MDN is **the worst** resource to understand about *prototype*. – overexchange Aug 20 '15 at 09:41
  • @overexchange If you're still learning, how you can do this bold statement? It's not the worst resource, and it works well. It's just you're still learning JS and you need to get familiar with a lot of concepts. – Matías Fidemraizer Aug 20 '15 at 09:43
  • am new to JS, but am not new to Python-docs/Java-JLS. So, I understand, what the problem is with MDN? topics like exp&operator are good to read in MDN – overexchange Aug 20 '15 at 09:46
  • @overexchange See how MDN summarizes prototype chain: **JavaScript objects are dynamic "bags" of properties (referred to as own properties). JavaScript objects have a link to a prototype object. When trying to access a property of an object, the property will not only be sought on the object but on the prototype of the object, the prototype of the prototype, and so on until either a property with a matching name is found or the end of the prototype chain is reached.** – Matías Fidemraizer Aug 20 '15 at 09:47
  • @overexchange Are you sure it's the worst resource? It's a perfect summary! – Matías Fidemraizer Aug 20 '15 at 09:47
3

It's actually memory usage. Here are some images I have created depicting each problem.

In the image below, each instance of person is linked to the same prototype object. This saves memory if multiple instances are created pointing to the same object. However, if you change 'Earth' to 'Mars' every instance will have the same change.

enter image description here


In the image below each instance will point to a completely different property linked specifically to that instance. If you believe a specific planet can change names, you should do this.. otherwise use prototype because this will use more resources.

enter image description here

kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131