var extend = function(protoProps, staticProps) { var parent = this; var child; if (protoProps && _.has(protoProps, 'constructor')) { child = protoProps.constructor; /* 重寫夠構造器函數 */ } else { child = function(){ return parent.apply(this, arguments); }; } /*定義child是子類的構造器函數*/ _.extend(child, parent, staticProps); /* 擴展 child的靜態屬性和方法,這裏默認全部子類有extend靜態方法,理解for(var i in function) */ var Surrogate = function(){ }; /* 在 new的時候重寫constructor屬性 */ Surrogate.prototype = parent.prototype; /* 即便這裏的constructor被污染了也沒有關係,拿到父類prototype的全部方法 */ child.prototype = new Surrogate(); /*由於 prototype以後 child.prototype.constructor指向child ,這裏必須是new一下 ,克隆child對象 */ if (protoProps) _.extend(child.prototype, protoProps); /*最後一步 添加子類的方法到 child的prototype上*/ child.__super__ = parent.prototype; return child; /*返回子類構造器函數*/ }; Model.extend = Collection.extend = Router.extend = View.extend = History.extend = extend; /*給Backbone.Model,Backbone.Collection, Backbone.Router,Backbone.View添加extend靜態方法。 函數是對象,此處引用*/