<script>
// 父类 Animal
var Animal = function() {
this.name = 'animal';
};
Animal.prototype = {
run: function() {
console.log(this.name + " run....");
},
eat: function() {
console.log(this.name + " eat...");
}
}
var Dog = function() {
Animal.call(this);
name = 'dog';
this.name = name;
}
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
// 覆盖
Dog.prototype.run = function() {
console.log(this.name + "...run...run...");
}
// test
var animal = new Animal();
animal.eat(); // animal eat...
var dog = new Dog();
dog.eat(); // dog eat...
dog.run(); // dog...run...run...
</script>
JavaScript原型继承的小例子
最新推荐文章于 2020-11-25 19:44:51 发布