Fork me on GitHub

原型继承

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)

posted on 2012-08-24 15:48  【艾伦】  阅读(367)  评论(0)    收藏  举报