-1

Here's an example from Javascript is sexy site:

function People () {
​this.superstar = "Michael Jackson";
}

People.prototype.athlete = "Tiger Woods";

​// Define "athlete" property on the People prototype so that "athlete" is
// accessible by all objects that use the People () constructor.

My question: What's the difference between inherit the property athlete from people.prototype.athlete like it would be inside People constructor this.athlete="Tiger Woods";.

In comments it says that athlete is accessible by all objects that use the People () constructor.

But when I put this property athlete like I said inside people constructor it also would be accessible by all objects that use the People ()constructor.

What's the difference between 1 and 2?

//1
function People () {
​this.superstar = "Michael Jackson";
}
People.prototype.athlete = "Tiger Woods";

//2
function People () {
​this.superstar = "Michael Jackson";
this.athlete = "Tiger Woods"; 
}

When I create object using example #1:

var person = new People();
console.log(person.hasOwnProperty('superstar')); //TRUE
console.log(person.hasOwnProperty('athlete'));  // FALSE

I know that athlete property is not own by person object.

But what are other differences?

Newd
  • 2,174
  • 2
  • 17
  • 31
PiotrX
  • 33
  • 3
  • That's basically the only difference. (and of course `Person.prototype.hasOwnProperty(…)`) – Bergi Jun 30 '15 at 13:09
  • However, the implications of this difference are vast… Try constructing some instances and then change `Person.prototype.athlete`, to understand what property inheritance actually *means*! – Bergi Jun 30 '15 at 13:11
  • possible duplicate of [Understanding prototypal inheritance in JavaScript](http://stackoverflow.com/questions/892595/understanding-prototypal-inheritance-in-javascript) – hazzik Jun 30 '15 at 13:15

1 Answers1

0

Short answer: memory sharing.

Because prototype holds value "Tiger Woods" only once, on the other hand, every instance of People will have its own value for superstar. In this case I think memory is usually optimized with strings (correct me if I'm wrong), but if it was an object for example, it would be a different story, a new object would be defined each time you instanciate People.

Also if you modify People.prototype.athlete, every default value for any instance of People (anywhere) would be the newly defined value (if you overrid it for one or several instances through, instance.athlete, well it doesn't matter).

axelduch
  • 10,769
  • 2
  • 31
  • 50
  • You said I quote "Also if you modify People.prototype.athlete, every default value for any instance of People (anywhere) would be the newly defined value" it's true but in 2 example if I modify this.athlete (inside constructor) it also changes value of any instances of People constructor. – PiotrX Jul 01 '15 at 13:22
  • The constructor is called only once per instance. Which means that you can't update People constructor (after instanciation), and hope for it to alter previously instanciated People, one the strengths of prototype is that if you modify it, past instances fields are also modified (if fields weren't overridden after instanciation on the instance directly). – axelduch Jul 01 '15 at 15:27
  • Thanks aduch but Could you give my some example of what you said for better understanding ? – PiotrX Jul 02 '15 at 10:22