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?