0

我有三个这样的主干视图:

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 的方法。

谢谢你。

4

3 回答 3

1

你实际上在做什么,如果你真的按照你的函数调用的步骤,你应该能够理解它确实是在无限循环中调用“孙子”视图:)

this提示:每次都想一想你apply......;)

否则,这可能是您要实现的目标:

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-26T19:12:21.637 回答
0

试一试:将此添加到您的 ChildView:

addUsers : function()
{
    var self = this;
    console.log("Child's Add users");
    self.constructor.__super__.addUsers.apply(self);
}

也许因为该addUsers函数在 中未正确定义,ChildView.prototype但它被继承,然后它找不到并在self.prototype. 我不知道..正如我所说的,我认为 JS 并不意味着以这种方式使用继承作为常见的面向 OO 的语言

于 2012-04-26T18:50:05.903 回答
0

我把你的代码转换成 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);

据我了解,棘手的一点是您将 this 绑定到函数内部的 self 。每次使用调用函数的上下文调用函数时都会发生这种情况,这正是您要省略的内容。您需要将 this 绑定到函数中,因为通常仅用于回调,或者在没有引用调用函数的对象的情况下,只有对函数的引用。因此,如果您需要在这种情况下绑定 this,请在函数之外进行。

于 2012-04-27T06:55:06.980 回答