在《Javascript Web Application》第3章——Model and Data中对模型层的封装,代码如下:
虽然代码看懂了,可这样封装的好处有哪些呢?而且如果不配图,很难理解这段代码,求牛人解析。
var Model = { inherited: function() {}, created: function() {}, prototype: { init: function() {} }, create: function() { var object = Object.create(this); object.parent = this; object.prototype = object.fn = Object.create(this.prototype); object.create(); this.inherited(object); return object; }, init: function() { var instance = Object.create(this.prototype); instance.parent = this; instance.init.apply(instance, arguments); return instance; } }
例子中用到的原型继承
if (typeof Object.create !== "function") Object.create = function(o) { function F(){} F.prototype = o; return new F(); }
创建具体模型
var Asset = Model.create();
创建具体模型实例
var asset = Asset.init();
对应的原型链图:
