- JS中实现继承,只需要改变函数的原型链即可
- 示例
function Cat(name){
this.name = name;
}
Cat.prototype.eat_fish = function(fish){
console.log(this.name, "在吃", fish);
};
function BosiCat(name){
this.name = name;
}
BosiCat.prototype.dance = function(){
console.log(this.name, "波斯猫会跳舞");
};
function YingDuanCat(name){
this.name = name;
}
YingDuanCat.prototype.sajiao = function(){
console.log(this.name, "英短猫撒娇");
};
// 继承关系
BosiCat.prototype.__proto__ = Cat.prototype;
// 波斯猫, 继承猫
var c1 = new BosiCat("xwl");
c1.dance();
// c1.__proto__.__proto__ = Cat.prototype;
c1.eat_fish("马步鱼"); // eat_fish 是 c1.__proto__.__proto__上的
// 英短猫, 继承猫
YingDuanCat.prototype.__proto__ = Cat.prototype;
var c2 = new YingDuanCat("杰克");
c2.sajiao();
c2.eat_fish('多春鱼');
标签:function,name,proto,继承,JavaScript26,fish,Cat,prototype
From: https://www.cnblogs.com/xwltest/p/17610280.html