<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原型繼承的小例子
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.