关于原型链的理解
这里有两个例子看到后让我觉得一目了然
var anmial = {
name: "anmial",
eat:function () {
console.log(this.name + " is eating.");
}
}
anmial.eat();//anmial is eating.
anmial.color = "black";
var dog = {
name: "dog",
__proto__:anmial //原型指向 anmial
}
var cat = {
name:"cat",
__proto__:anmial
}
dog.eat(); //dog is eating.
cat.eat(); //cat is eating
function Student(name){
this.name=name;
}
Student.prototype={
sayHello:function () {
console.log("Hi,I'm " + this.name);
}
}
var andy=new Student("andy");