0

次のような 3 つのバックボーン ビューがあります。

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 メソッドを呼び出すと、最大スタック サイズを超えてスローされます。これは、自分自身を呼び出し続けているためだと思います。しかし、把握することはできません。その理由はスーパーのメソッドを呼び出しているようです。

ありがとうございました。

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

私はあなたのコードを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);

私の理解では、トリッキーな点は、これを関数内の自己にバインドしていることです。これは、関数を呼び出しているコンテキストで関数を呼び出すたびに発生します。これは、まさに省略しようとしているものです。通常はコールバックの場合、または関数を呼び出すオブジェクトへの参照がなく、関数への参照のみがある場合にのみ行うため、これを関数にバインドする必要があります。したがって、そのような状況でこれをバインドする必要がある場合は、関数の外で実行してください。

于 2012-04-27T06:55:06.980 に答える
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がこのように継承を一般的なオブジェクト指向言語として扱うことを意味しているとは思いません。

于 2012-04-26T18:50:05.903 に答える