您的位置:首页 > Web前端 > JavaScript

JavaScript原型继承的小例子

2016-11-11 18:49 603 查看
<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 继承 函数