这些 Backbone/Underscore .bind() 方法有什么区别?

发布于 2024-11-30 02:46:33 字数 631 浏览 1 评论 0原文

window.SomeView = Backbone.View.extrend({
    initialize1: function() {
        _.bindAll(this, 'render');
        this.model.bind('change', this.render);
    },

    initialize2: function() {
        this.model.bind('change', _.bind(this.render, this));
    },

    initialize3: function() {
        _.bind(this.render, this);
        this.model.bind('change', this.render);
    },
});

在一些 SO 成员的帮助下,我能够让我的测试项目使用绑定方法in​​itialize1和initialize2;我不明白的是为什么initialize3不起作用?

文档: _.bind(function, object, [*arguments])

window.SomeView = Backbone.View.extrend({
    initialize1: function() {
        _.bindAll(this, 'render');
        this.model.bind('change', this.render);
    },

    initialize2: function() {
        this.model.bind('change', _.bind(this.render, this));
    },

    initialize3: function() {
        _.bind(this.render, this);
        this.model.bind('change', this.render);
    },
});

With help from some SO members, I was able to get my test project working with binding methods initialize1 and initialize2; what I don't understand is why initialize3 doesn't work?

documentation: _.bind(function, object, [*arguments])

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

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

发布评论

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

评论(1

萌辣 2024-12-07 02:46:33

主要有以下三个区别: _.bind 一次仅适用于一种方法,允许柯里化,并且返回 绑定函数(这也意味着您可以在匿名函数上使用 _.bind):

函数绑定到对象,这意味着每当调用函数时,this的值将是对象。 (可选)将参数绑定到函数以预先填充它们,也称为柯里化

_.bindAll 一次绑定许多命名方法,不允许柯里化,并将它们绑定到位:

对象上绑定由methodNames指定的许多方法,以便在调用这些方法时在该对象的上下文中运行。

所以这两段代码大致是等价的:

// Bind methods (not names) one a time.
o.m1 = _.bind(o.m1, o);
o.m2 = _.bind(o.m2, o);

// Bind several named methods at once.
_.bindAll(o, 'm1', 'm2');

但是没有与此等效的 bindAll

f = _.bind(o, o.m1, 'pancakes');

这使得 f()o.m1('pancakes ') (这是柯里化)。


因此,当您这样说时:

_.bindAll(this, 'render');
this.model.bind('change', this.render);

您将方法 render 绑定为具有与当前 this 匹配的 this,然后绑定this.renderthis.model 上的更改事件。

当你这样说时:

this.model.bind('change', _.bind(this.render, this));

你也在做同样的事情。而这个:

_.bind(this.render, this);
this.model.bind('change', this.render);

不起作用,因为你扔掉了 _.bind 的返回值(即你扔掉了绑定函数)。

There are three main differences; _.bind only works on one method at a time, allows currying, and returns the bound function (this also means that you can use _.bind on an anonymous function):

Bind a function to an object, meaning that whenever the function is called, the value of this will be the object. Optionally, bind arguments to the function to pre-fill them, also known as currying.

whereas _.bindAll binds many named methods at once, doesn't allow currying, and binds the them in-place:

Binds a number of methods on the object, specified by methodNames, to be run in the context of that object whenever they are invoked.

So these two chunks of code are roughly equivalent:

// Bind methods (not names) one a time.
o.m1 = _.bind(o.m1, o);
o.m2 = _.bind(o.m2, o);

// Bind several named methods at once.
_.bindAll(o, 'm1', 'm2');

But there is no bindAll equivalent to this:

f = _.bind(o, o.m1, 'pancakes');

That makes f() the same as o.m1('pancakes') (this is currying).


So, when you say this:

_.bindAll(this, 'render');
this.model.bind('change', this.render);

You're binding the method render to have a this that matches the current this and then you're binding this.render to the change event on this.model.

When you say this:

this.model.bind('change', _.bind(this.render, this));

You're doing the same thing. And this:

_.bind(this.render, this);
this.model.bind('change', this.render);

doesn't work because you're throwing away the return value of _.bind (i.e. you throw away the bound function).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文