var Model = { inherited:function () { }, created:function () { }, prototype:{ init:function () { } }, //給類添加屬性 extend:function (obj) { var extended = obj.extended; for (var i in obj) { this[i] = obj[i]; } if (extended) extended(klass); }, //給實列添加屬性 include:function (obj) { var included = obj.included; for (var i in obj) { this.prototype[i] = obj[i]; } //觸發回調 if (included) included(klass); }, 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; }}//儲存資來源物件Model.records = {};//持久化記錄Model.include({ newRecord:true, create:function () { this.newRecord = false; this.parent.records[this.id] = this; }, destroy:function () { delete this.parent.records[this.id]; }, update:function () { this.parent.records[this.id] = this; }, save:function () { this.newRecord ? this.create() : this.update(); }, find:function (id) { return this.records[id]; }})//繼承的父類model的一個建構函式var Asset = Model.create();//執行個體對象var asset1 = Asset.init();asset1.name = "Aaron-1";asset1.id = 1;asset1.save();var asset2 = Asset.init();asset2.name = "Aaron-2";asset2.id = 2;asset2.save();console.log( asset1)