原型继承
if (typeof Object.create !== "function") {
    Object.create = function (o) {
        function F() {
        }

        F.prototype = o;
        return new F();
    }
}


var Model = {
    inherited:function () {
    },
    created:function () {
    },

    prototype:{
        init:function () {
        }
    },

    create:function () {
        //子类 返回一个新对象,继承自model对象,创建新模型
        var object = Object.create(this);
        //指向父类
        object.parent = this;
        //子类原型方法
        object.prototype = object.fn = Object.create(this.prototype);

        object.created();
        this.inherited(object);
        return object;
    },

    init:function () {
        //返回一个新对喜爱那个,继承自model.prototype -> model对象的一个实例
        var instance = Object.create(this.prototype);
        instance.parent = this;
        instance.init.apply(instance, arguments);
        return instance;
    }

}

var User = Model.create();
var user = User.init();
console.log(User ,user)
原型继承

原型继承


本文转自艾伦 Aaron博客园博客,原文链接:http://www.cnblogs.com/aaronjs/archive/2012/08/24/2654342.html,如需转载请自行联系原作者

相关文章: