Backbone.js 为什么在模型中使用returnparent.apply(这个,参数)
我不知道为什么要使用它Backbone.js 为什么在模型中使用returnparent.apply(这个,参数),backbone.js,Backbone.js,我不知道为什么要使用它returnparent.apply(这个,参数)来继承 // The constructor function for the new subclass is either defined by you // (the "constructor" property in your `extend` definition), or defaulted // by us to simply call the parent constructor. if (protoProp
returnparent.apply(这个,参数)
来继承
// The constructor function for the new subclass is either defined by you
// (the "constructor" property in your `extend` definition), or defaulted
// by us to simply call the parent constructor.
if (protoProps && _.has(protoProps, 'constructor')) {
child = protoProps.constructor;
} else {
child = function(){
return parent.apply(this, arguments);
};
}
// Add static properties to the constructor function, if supplied.
_.extend(child, parent, staticProps);
// Set the prototype chain to inherit from `parent`, without calling
// `parent` constructor function.
var Surrogate = function() {
this.constructor = child;
};
Surrogate.prototype = parent.prototype;
child.prototype = new Surrogate;
这与Javascript对象扩展和继承的工作方式密切相关。在Javascript中,要使用新属性扩展对象,必须扩展其基本原型 下面是一个使用最广泛、最优雅的继承方法的小片段。还有其他原型继承方法,最著名的是Crockford的继承方法(见下文) Crockford继承方法:
function object(o) {
function F() {}
F.prototype = o;
return new F();
}
回到你的问题上来。apply
和call
之间的唯一区别在于,第一种方法需要一组参数作为参数。使用apply,您可以使用数组文字,例如
fun.apply(this, ['eat', 'bananas'])
或数组对象,例如:
fun.apply(this, new Array('eat', 'bananas')).
有关详细说明,请查看本文:
extend
函数执行相同的操作:使用额外的参数扩展父对象
return this
返回已调用方法的对象实例,并用于方法链接。因此,您可以在同一实例化对象上同时调用多个方法:
var surrogate = new Surrogate();
surrogate.copy().delete();
阅读并理解它的作用,看看它是否更有意义。谢谢你的帮助,我来自中国,时间很短。我会说一点英语,如果我不归还这个。有什么不同?如果你认为我的回答确实满足你的问题,请接受。
var surrogate = new Surrogate();
surrogate.copy().delete();