js 대상을 향한 계승 - 원형 계승

12276 단어 대상을 향하다
//animal    
        var Animal = function(name)
        {
            this.name = name;
            this.sayhello = function()
            {
                alert("HI, " + this.name + ", ?");
            };
        };
        Animal.prototype.shout = function()
        {
            alert(this.name + ", !");
        };
        Animal.prototype.game = function()
        {
            alert(this.name + ", !");
        };
        
        var Dog = function(name)
        {
            //this.name = name;
            this.name = name;
            this.shout = function()// 
            {
                alert(this.name + ", !");
            };
        };
        
        var Cat = function(name)
        {
            this.name = name;
            this.shout = function()
            {
                alert(this.name + ", !");
            };
        };
        
        // 
        Dog.prototype = Cat.prototype = new Animal();
        
        var xh = new Dog(" ");
        xh.sayhello();
        xh.shout();
        xh.game();
        
        var xm = new Cat(" ");
        xm.sayhello();
        xm.shout();
        xm.game();

함수 봉인 후:
var inherit = function (subclass, superclass) {
    subclass.prototype = new superclass();
};
 
//animal    
var Animal = function (name) {
    this.name = name;
    this.sayhello = function () {
        alert("HI, " + this.name + ", ?");
    };
};
Animal.prototype.shout = function () {
    alert(this.name + ", !");
};
Animal.prototype.game = function () {
    alert(this.name + ", !");
};
 
var Dog = function (name) {
    //this.name = name;
    this.name = name;
    this.shout = function () // 
    {
        alert(this.name + ", !");
    };
};
 
var Cat = function (name) {
    this.name = name;
    this.shout = function () {
        alert(this.name + ", !");
    };
};
 
// 
//Dog.prototype = Cat.prototype = new Animal();
inherit(Dog, Animal);
inherit(Cat, Animal);
 
var xh = new Dog(" ");
xh.sayhello();
xh.shout();
xh.game();
 
var xm = new Cat(" ");
xm.sayhello();
xm.shout();
xm.game();

함수에 extends 방법 추가
Function.prototype.extends = function (superclass) {
    this.prototype = new superclass();
};
 
//animal    
var Animal = function (name) {
    this.name = name;
    this.sayhello = function () {
        alert("HI, " + this.name + ", ?");
    };
};
Animal.prototype.shout = function () {
    alert(this.name + ", !");
};
Animal.prototype.game = function () {
    alert(this.name + ", !");
};
 
var Dog = function (name) {
    this.name = name;
    this.shout = function () // 
    {
        alert(this.name + ", , !");
    };
};
Dog.extends(Animal);
 
var Cat = function (name) {
    this.name = name;
    this.shout = function () {
        alert(this.name + ", !");
    };
};
Cat.extends(Animal);
 
// 
//Dog.prototype = Cat.prototype = new Animal();
/*inherit(Dog, Animal);
        inherit(Cat, Animal);*/
//Dog.extends(Animal);
//Cat.extends(Animal);
 
/*var xh = new Dog(" ");
        xh.sayhello();
        xh.shout();
        xh.game();
         
        var xm = new Cat(" ");
        xm.sayhello();
        xm.shout();
        xm.game();*/
 
var Husky = function (name, color, sex) {
    this.name = name;
    this.color = color;
    this.sex = sex;
    this.sayhello = function () {
        alert("Hello, " + this.sex + " , , :“" + this.name + "”, ?");
    };
    this.showcolor = function () {
        alert(this.color);
    };
    /*this.shout = function()// 
            {
                alert(this.name + ", !");
            };*/
};
Husky.extends(Dog);
 
var xh = new Husky(" ", " ", " ");
xh.sayhello();
xh.shout();
xh.game();
xh.showcolor();

좋은 웹페이지 즐겨찾기