js 类的继承

作者: ccw1078 | 来源:发表于2017-11-12 17:31 被阅读0次
    1. 方法一:
      通过使用构造函数,prototype,inherit 和 method 方法来实现类的继承;
    //定义一个通用的 clone 函数,用来克隆一个新对象,该新对象以参数传入 的对象为原型;
    function clone(object){
        function OneShotConstructor(){};
        OneShotConstructor.prototype = object; // 将对象做为构造函数的原型;
        return new OneShotConstructor();
    }
    
    //定义 inherit 和 method 方法
    
    // 通过参数传入构造函数,以其原型克隆出一个对象,并将其作为当前对象的原型,实现了继承;
    Object.prototype.inherit = function(baseConstructor){
        this.prototype = clone(baseConstructor.prototype);
        this.prototype.constructor = this;
    }
    
    
    // 给对象定义了一些 method 的方法,该方法使得对象可以将传入的函数,添加成为它自己的方法;
    Object.prototype.method = function(name, func){
        this.prototype[name] = func;
    }
    
    //写个例子
    function Item(name){
        this.name = name;
    };
    
    Item.prototype.inspect = function(){
        alert("It is " + this.name + ".");
    };
    
    Item.prototype.kick = function(){
        alert("Klunk!");
    };
    
    Item.prototype.take = function(){
        alert("you cannot lift " + this.name + ".");
    };
    
    var lantern = new Item("the brass lantern");
    
    function DetailedItem(name, details){
        this.name = name;
        this.details = details;
    };
    
    DetailedItem.inherit(Item);
    
    DetailedItem.method("inspect", function(){
        alert("you see " + this.name + "," + this.details + ".");
    });
    
    var giantSloth = new DetailedItem("the giant sloth",
                                        "it is quietly hanging from a tree, munching leaves");
                                        
    function SmallItem(name){
        this.name = name;
    };
    
    SmallItem.inherit(Item);
    
    SmallItem.method("kick", function(){
        alert(this.name + " files across the room.");
    });
    
    SmallItem.method("take", function(){
        alert("you take " + this.name + ".");
    });
    
    var pencil = new SmallItem("the red pencil");
    pencil.kick();
    
    1. 方法二:
      把原型放到一个对象中做为类,然后通过 create 方法来实例化,通过 extend 来创建子类;
      这种方法的好处是可以忽略 prototype 的使用;
    //定义 extend 和 create 方法
    
    // 给对象定义了一个 create 方法,该方法使得对象可以复制一个子对象出来;并且这个子对象还会
    // 根据传入的参数,调用继承自父对象的 construct 方法,进行自己的初始化;
    Object.prototype.create = function(){
        var object = clone(this); 
        if (object.construct != undefined)
            object.construct.apply(object, arguments); 
        return object;
    }
    
    
    // 给对象定义了一个 extend 方法,该方法会创建一个子对象,并将传入的对象的所有属性,复制一份到子对象上;
    Object.prototype.extend = function(properties){
        var result = clone(this);
        forEachIn(properties, function(name, value){
            result[name] = value;
        });
        return result;
    }
    
    
    
    //写个相同的例子
    var Item = {
        construct: function(name){
            this.name = name;
        },  
        inspect: function(){
            alert("It is " + this.name + ".");
        },  
        kick: function(){
            alert("Klunk!");
        },
        take: function(){
            alert("You cannot lift " + this.name + ".");
        }
    }
    
    var lantern = Item.create("the brass lantern");
    
    var DetailedItem = Item.extend({
        construct: function(name, details){
            Item.construct.call(this, name);
            this.details = details;
        },
        inspect: function(){
            alert("you see " + this.name + "," + this.details + ".");
        }
    });
    
    var giantSloth = DetailedItem.create("the giant sloth", 
                                        "it is quietly hanging from a tree, munching leaves");
                                        
    var SmallItem = Item.extend({
        kick: function(){
            alert(this.name + " files across the room.");
        },
        take: function(){
            alert("you take " + this.name + ".");
        }
    });
    
    var pencil = SmallItem.create("the red pencil");
    pencil.take();
    
    
    总结:不管是第1种的 inherit 方法,还是第二种的 create 方法,它们都是通过将父对象做子对象的原型来实现的继承,区别在后者对 prototype 的使用进行了封装,不需要老是打 protoype 这个单词, 降低了出错概率,更好的实现了概念的抽象;

    相关文章

      网友评论

        本文标题:js 类的继承

        本文链接:https://www.haomeiwen.com/subject/zrkomxtx.html