如果我使用 _.bindAll(this) 为什么我的 Backbone.View.constructor.__super__ 未定义
我正在子类化我自己的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么不简单地使用它来调用超级:(
为了澄清起见,我将其分成几行,您可以在一行中进行调用)
参考: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)
Reference: GetPrototypeOf
鉴于这个问题只有解决方案而没有解释,我将尝试提供一个......
当 Underscore 的
bindAll
方法使用单个参数(对象)调用,该对象的所有函数类型属性不再引用原始函数,而是引用另一个修复上下文的函数。由于函数类型的对象属性之一是
constructor
,它是对Backbone构造函数的引用(具有属性__super__
),因此该属性将被新的覆盖。功能。这意味着object.constructor
将不再具有属性__super__
。为了解决这个问题,我使用以下函数作为 Underscore 的
bindAll
的替代函数:它与 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 thatobject.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
:It's almost identical to Underscore's version but adds any properties of the original function to the new function through use of
_.extend()
.我用 getConstructor() 修补了 Backbone,它返回构造函数并且不受 _.bindAll 的影响。
用法:
实施:
I patched Backbone with getConstructor(), which returns the constructor and is immune to _.bindAll.
Usage:
Implementation:
我是 Backbone 的新手,但在另一个框架上有超过 4 年的经验,该框架也希望您将包含方法的对象传递给扩展方法,但问题是这没有提供足够细粒度的控制来对其中包含的方法进行操作正在传递的对象。
要获得更多控制,您可以传递一个闭包来扩展,并且闭包可以返回一个对象。一般来说,这种技术可以实现更细粒度的控制和更大的复杂性,并且可以用来解决您的特定问题。
在返回方法的闭包内,这些方法可以分为两大类:
如果我们从其自己的单独对象返回特定于我们的应用程序的自定义方法,则我们可以使用 _.bind 将 _.bindAll 的“部分应用”仅用于那些自定义方法名称。
那么把它们放在一起:
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:
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: