使用call和apply调用函数有什么区别?
var func = function() {
alert('hello!');
};
Run Code Online (Sandbox Code Playgroud)
func.apply(); VS func.call();
上述两种方法之间是否存在性能差异?当它最好使用call过apply,反之亦然?
我需要initialize从继承的MyModel类中调用父类的方法,而不是像我今天那样完全覆盖它.
我怎么能这样做?
这是我的代码现在看起来的样子:
BaseModel = Backbone.Model.extend({
initialize: function(attributes, options) {
// Do parent stuff stuff
}
});
MyModel = BaseModel.extend({
initialize: function() {
// Invoke BaseModel.initialize();
// Continue doing specific stuff for this child-class.
},
});
Run Code Online (Sandbox Code Playgroud) 我有一个三个Backbone View类继承:
var preventRecursion = 0;
var parentView = Backbone.View.extend({
initialize: function(){
console.log('parentView');
}
});
var nestedChildView = parentView.extend({
initialize: function(){
if (++preventRecursion == 5) {throw "Recursion"};
console.log('nestedChildView');
this.constructor.__super__.initialize.apply(this);
}
});
var nestedNestedChildView = nestedChildView.extend({
initialize: function(){
console.log('nestedNestedChildView');
this.constructor.__super__.initialize.apply(this);
}
});
Run Code Online (Sandbox Code Playgroud)
当我尝试创建nestedNestedChildView时:
var t = new nestedNestedChildView();
Run Code Online (Sandbox Code Playgroud)
我得到无限递归:这是jsfiddle
我有一个观点:
var MultiSelectCompositeView = Backbone.Marionette.CompositeView.extend({
events: {
'click .listItem': 'setSelectedOnClick'
},
onRender: function () {
console.log("MultiSelectCompositeView onRender has executed.");
}
});
Run Code Online (Sandbox Code Playgroud)
我有另一个扩展MultiSelectCompositeView的视图:
var VideoSearchView = MultiSelectCompositeView.extend({
events: _.extend(MultiSelectCompositeView.prototype.events, {
'input @ui.searchInput': 'showVideoSuggestions',
'click button#hideVideoSearch': 'destroyModel',
'contextmenu @ui.videoSearchResults': 'showContextMenu'
},
onRender: function () {
this.ui.playlistActions.append((new PlaySelectedButtonView()).render().el);
this.ui.playlistActions.append((new SaveSelectedButtonView()).render().el);
// TODO: Is there a better way to do this?
MultiSelectCompositeView.prototype.onRender.call(this, arguments);
}
});
Run Code Online (Sandbox Code Playgroud)
我对VideoSearchView不会隐式扩展MultiSelectCompositeView的事件以及VideoSearchView必须手动调用MultiSelectCompositeView的onRender方法这一事实感到不满.
Backbone.Marionette有什么东西可以让我以更加无缝的方式扩展我的自定义视图吗?
所以我现在有这样的情况:
app.Ui.ModalView = Backbone.View.extend({
events: {
},
initialize: function() {
},
render: function() {
var that = this;
var model = this.model.toJSON();
that.$el.html(that.template(_.extend(this.params || {}, {
model: model,
})));
return this;
}
});
Run Code Online (Sandbox Code Playgroud)
然后是继承的视图:
app.Views.childView = kf.Ui.ModalView.extend({
template: JST["templates/app/blah/blah-edit.html"],
events: {
},
initialize: function() {
var that = this;
this.events = _.extend({}, app.Ui.ModalView.prototype.events, this.events);
app.Ui.ModalView.prototype.initialize.apply(this, arguments);
},
render: function(){
// add extra logic in this render function, to run as well as the inherited render function?
}
});
Run Code Online (Sandbox Code Playgroud)
所以,我不想覆盖父级render() …
backbone.js ×4
javascript ×3
inheritance ×2
class ×1
dynamic ×1
function ×1
marionette ×1
oop ×1
performance ×1