我有三个这样的基本观点:
ParentView = Backbone.View.extend({
addUsers : function()
{
console.log("Parent's Add User");
},
addProject : function()
{
console.log("Parent's Add Project");
}
});
ChildView = ParentView.extend({
addProject : function()
{
var self = this;
console.log("Child's add Project");
self.constructor.__super__.addProject.apply(self);
}
});
GrandChildView = ChildView.extend({
addItem : function()
{
var self = this;
self.addProject();
},
addUsers : function()
{
var self = this;
console.log("Grand Child's Add users");
self.constructor.__super__.addUsers.apply(self);
}
});
var vChild = new ChildView();
vChild.addProject(); // works fine, by calling it own and parent's functions.
var vGrandChild = new GrandChildView();
vGrandChild.addUsers(); // This throws Maximum call stack size exceeded error,当我创建GrandChildView的新实例,然后调用它的addUsers方法时,它抛出了超出最大堆栈大小的异常,我猜这是因为它一直在调用自己。但却不能弄清楚。原因似乎是调用了super的方法。
谢谢。
发布于 2012-04-27 03:12:22
你实际在做什么,如果你真的遵循函数调用的步骤,你应该能够理解,实际上是在无限循环中调用“孙子”视图:)
提示:每次在apply时思考this是什么是值得的…;)
否则,这可能就是您要实现的目标:
ParentView = Backbone.View.extend({
addUsers : function()
{
console.log("Parent's Add User");
},
addProject : function()
{
console.log("Parent's Add Project");
}
});
ChildView = ParentView.extend({
addProject : function()
{
console.log("Child's add Project");
ParentView.prototype.addProject.call(this);
}
});
GrandChildView = ChildView.extend({
addItem : function()
{
this.addProject();
},
addUsers : function()
{
console.log("Grand Child's Add users");
ChildView.prototype.addUsers.call(this);
}
});
var vChild = new ChildView();
vChild.addProject(); // works fine, by calling it own and parent's functions.
var vGrandChild = new GrandChildView();
vGrandChild.addUsers(); 发布于 2012-04-27 02:50:06
试一试:将这个添加到你的ChildView中:
addUsers : function()
{
var self = this;
console.log("Child's Add users");
self.constructor.__super__.addUsers.apply(self);
}也许是因为addUsers函数没有在ChildView.prototype中正确定义,但是它被继承了,所以在self.prototype中找不到它并进行中继。我不知道..正如我所说的,我认为JS并不打算像普通的面向对象语言那样使用继承。
发布于 2012-04-27 14:55:07
我取了你的代码,转换成CoffeeScript,它给了我这个JavaScript,它对我很有效:
var ChildView, GrandChildView, ParentView,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; },
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
ParentView = (function(_super) {
__extends(ParentView, _super);
ParentView.name = 'ParentView';
function ParentView() {
return ParentView.__super__.constructor.apply(this, arguments);
}
ParentView.prototype.addUsers = function() {
return console.log("Parent's Add User");
};
ParentView.prototype.addProject = function() {
return console.log("Parent's Add Project");
};
return ParentView;
})(Backbone.View);
ChildView = (function(_super) {
__extends(ChildView, _super);
ChildView.name = 'ChildView';
function ChildView() {
this.addProject = __bind(this.addProject, this);
return ChildView.__super__.constructor.apply(this, arguments);
}
ChildView.prototype.addProject = function() {
console.log("Child's add Project");
return ChildView.__super__.addProject.apply(this, arguments);
};
return ChildView;
})(ParentView);
GrandChildView = (function(_super) {
__extends(GrandChildView, _super);
GrandChildView.name = 'GrandChildView';
function GrandChildView() {
this.addUsers = __bind(this.addUsers, this);
this.addItem = __bind(this.addItem, this);
return GrandChildView.__super__.constructor.apply(this, arguments);
}
GrandChildView.prototype.addItem = function() {
return this.addProject();
};
GrandChildView.prototype.addUsers = function() {
console.log("Grand Child's Add users");
return GrandChildView.__super__.addUsers.apply(this, arguments);
};
return GrandChildView;
})(ChildView);据我所知,棘手的一点是你将它绑定到函数中的self。每次调用函数时,都会发生这种情况,而这正是您试图省略的。你需要在函数中绑定它,因为你这样做通常只是为了回调,或者当你没有对正在调用函数的对象的引用时,只有对函数的引用。因此,如果您需要在这种情况下绑定此函数,请在函数外部进行绑定。
https://stackoverflow.com/questions/10334116
复制相似问题