Animal just a function
'function' object has a property 'name'
for example
function app(){
console.log("hello")
}
console.log(app.name) // "app"
function Animal(name){
this.name = name;
}
console.log(Animal.name) // "Animal"
so Animal object has a property 'name', function object also has a property 'name'
you can try this
function Animal(name) {
this.notname = name; //do not use "name" as a property name
}
function Rabbit(speed) {
Animal.call(this, 'Rabbit');
this.speed = speed;
}
Rabbit.prototype = Animal;
var rabbit = new Rabbit(50);
console.log(rabbit.notname, rabbit.speed);
or
function Animal(name) {
this.name = name;
}
function Rabbit(speed) {
Animal.call(this, 'Rabbit');
this.speed = speed;
}
Rabbit.prototype = new Animal();
//you should point the Rabbit.prototype to a Animal object,not a function
var rabbit = new Rabbit(50);
console.log(rabbit.name, rabbit.speed);
Rabbit.prototype = Animal;part from? It should beRabbit.prototype = Object.create(Animal.prototype)