js 面向对象

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();



你可能感兴趣的:(js 面向对象)