如果我使用 _.bindAll(this) 为什么我的 Backbone.View.constructor.__super__ 未定义

发布于 2024-12-25 11:50:11 字数 407 浏览 1 评论 0原文

我正在子类化我自己的 Backbone.View。如果,在超类的初始化函数中,我写:

_.bindAll(this, 'many', 'methods');

并指定我想要绑定到此上下文的方法,我可以通过以下方式从子类调用 super:

this.constructor.__super__.initialize.apply(this, arguments);

但是,如果在超类中,我使用:

_.bindAll(this)

相反,当我从子类调用 super 时,

this.constructor.__super__

是未定义的。有什么智慧可以解释为什么会这样吗?

I am subclassing my own Backbone.View. If, in the super class' initialize function, I write:

_.bindAll(this, 'many', 'methods');

And specify the methods that I want to bind to this context, I can call super from the subclass via:

this.constructor.__super__.initialize.apply(this, arguments);

But, if in the super class, I use:

_.bindAll(this)

instead, when I go to call super from my subclass,

this.constructor.__super__

is undefined. Any wisdom on why that is?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空城缀染半城烟沙 2025-01-01 11:50:12

为什么不简单地使用它来调用超级:(

为了澄清起见,我将其分成几行,您可以在一行中进行调用)

var thisProto = Object.getPrototypeOf(thisInstance);
var superProto = Object.getPrototypeOf(thisProto);
superProto.superMethod.apply(thisInstance, [param1, param2]);

参考:GetPrototypeOf

Why not simply use this to call the super:

(I am separating to several lines for clarification, you can do the call in one line)

var thisProto = Object.getPrototypeOf(thisInstance);
var superProto = Object.getPrototypeOf(thisProto);
superProto.superMethod.apply(thisInstance, [param1, param2]);

Reference: GetPrototypeOf

亽野灬性zι浪 2025-01-01 11:50:12

鉴于这个问题只有解决方案而没有解释,我将尝试提供一个......

当 Underscore 的 bindAll 方法使用单个参数(对象)调用,该对象的所有函数类型属性不再引用原始函数,而是引用另一个修复上下文的函数。

由于函数类型的对象属性之一是constructor,它是对Backbone构造函数的引用(具有属性__super__),因此该属性将被新的覆盖。功能。这意味着 object.constructor 将不再具有属性 __super__

为了解决这个问题,我使用以下函数作为 Underscore 的 bindAll 的替代函数:

function safeBindAll(obj) {
    var funcs = Array.prototype.slice.call(arguments, 1);

    if (funcs.length == 0) {
        funcs = _.functions(obj);
    }

    _.each(funcs, function(f) {
        var oldProps = obj[f];
        obj[f] = _.bind(obj[f], obj);

        _.extend(obj[f], oldProps);
    });

    return obj;
}

它与 Underscore 的版本几乎相同,但通过使用 _.extend()

Seeing as there's only solutions to this problem but not explanations, I'm going to attempt to supply one...

When Underscore's bindAll method is invoked with the single argument (the object), all function type properties of that object no longer reference the original function but instead another that fixes the context.

Since one of the object's properties of type function is constructor, which is a reference to the Backbone constructor function (with the property __super__), the property will be overwritten with a new function. This then means that object.constructor will no longer have a property __super__.

To work around this issue I used the following function as an alternative to Underscore's bindAll:

function safeBindAll(obj) {
    var funcs = Array.prototype.slice.call(arguments, 1);

    if (funcs.length == 0) {
        funcs = _.functions(obj);
    }

    _.each(funcs, function(f) {
        var oldProps = obj[f];
        obj[f] = _.bind(obj[f], obj);

        _.extend(obj[f], oldProps);
    });

    return obj;
}

It's almost identical to Underscore's version but adds any properties of the original function to the new function through use of _.extend().

平安喜乐 2025-01-01 11:50:12

我用 getConstructor() 修补了 Backbone,它返回构造函数并且不受 _.bindAll 的影响。

用法:

this.getConstructor().__super__;

实施:

(function() {
  var backboneExtend = Backbone.Model.extend;
  var constructorExtend = function() {
    var child = backboneExtend.apply(this, arguments);
    child.prototype.getConstructor = function() {
      return child;
    };
    return child;
  };
  Backbone.Model.extend = Backbone.Collection.extend = Backbone.Router.extend = Backbone.View.extend = constructorExtend;
})();

I patched Backbone with getConstructor(), which returns the constructor and is immune to _.bindAll.

Usage:

this.getConstructor().__super__;

Implementation:

(function() {
  var backboneExtend = Backbone.Model.extend;
  var constructorExtend = function() {
    var child = backboneExtend.apply(this, arguments);
    child.prototype.getConstructor = function() {
      return child;
    };
    return child;
  };
  Backbone.Model.extend = Backbone.Collection.extend = Backbone.Router.extend = Backbone.View.extend = constructorExtend;
})();
清风夜微凉 2025-01-01 11:50:12

我是 Backbone 的新手,但在另一个框架上有超过 4 年的经验,该框架也希望您将包含方法的对象传递给扩展方法,但问题是这没有提供足够细粒度的控制来对其中包含的方法进行操作正在传递的对象。

要获得更多控制,您可以传递一个闭包来扩展,并且闭包可以返回一个对象。一般来说,这种技术可以实现更细粒度的控制和更大的复杂性,并且可以用来解决您的特定问题。

在返回方法的闭包内,这些方法可以分为两大类:

  1. 特定于 Backbone 的,例如构造函数和构造函数。初始化
  2. 特定于您的应用程序的自定义方法

如果我们从其自己的单独对象返回特定于我们的应用程序的自定义方法,则我们可以使用 _.bind 将 _.bindAll 的“部分应用”仅用于那些自定义方法名称。

那么把它们放在一起:

var foobar = Backbone.Model.extend(function() {
    var foobarMethods = modelMethods();

    return _.extend({}, foobarMethods, {
        constructor: function() {
            _.bind(_.bindAll, this, _.keys(foobarMethods));
            // equivalent to _.bindAll(this, 'foo', 'bar') except that the above
            // allow methods to be arbitrarily added or removed
        },
        initialize : function() {}          
    });

    //It's possible to "mixin" these methods from another file
    function modelMethods() {
        return {
            foo: function() {},
            bar: function() {},
        }
    }
}());

I'm new to Backbone but have over 4 years experience with another framework that also expects you to pass an object containing methods to an extend method, but the problem is this does not give sufficiently fine-grained control to act upon the methods contained within the object being passed.

To gain more control you can instead pass a closure to extend, and the closure can return an object. This technique in general allows finer-grained control and more potential for sophistication, and can be leveraged to solve your specific issue.

Inside the closure that returns the methods, those methods can be split into two broad categories:

  1. Backbone-specific, e.g. constructor & initialize
  2. Custom, specific to your application

If we return the custom methods specific to our application from their own separate object, we can then use _.bind for "partial application" of _.bindAll to just those custom method names.

Putting it all together then:

var foobar = Backbone.Model.extend(function() {
    var foobarMethods = modelMethods();

    return _.extend({}, foobarMethods, {
        constructor: function() {
            _.bind(_.bindAll, this, _.keys(foobarMethods));
            // equivalent to _.bindAll(this, 'foo', 'bar') except that the above
            // allow methods to be arbitrarily added or removed
        },
        initialize : function() {}          
    });

    //It's possible to "mixin" these methods from another file
    function modelMethods() {
        return {
            foo: function() {},
            bar: function() {},
        }
    }
}());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文