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

js 面向对象

2015-12-31 11:19 513 查看
function Animal(name) {
this.name = name;
}

Animal.prototype.eat = function(food) {
console.log("food");
};

Animal.prototype.getName = function()
{
return this.name;
};

var a = new Animal('hello');

a.eat("world");
console.log(a.getName());
console.log(a.name);

function Ferret(){}
Ferret.prototype = new Animal();//Ferret.prototype.__proto__ = Animal.prototype;
Ferret.prototype.type = "Domestic";

Ferret.prototype.eat = function (food) {
Animal.prototype.eat.call(this,food);

//
console.log("Ferret Eat:..");
};

var f = new Ferret();
f.eat();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: