下划线bindAll:保留“this”语境

发布于 2024-12-12 07:40:39 字数 923 浏览 6 评论 0原文

当调用 onSubmit 时,我在尝试将 uploader 保留为 this 上下文时遇到问题。有哪位 JS 高手可以帮忙吗?

uploader: {

        init: function(){
            var that = this;

            var fileUploader = new Uploader.FileUploaderBasic({
                    button      : $("#upload-btn")[0],
                    action      : "/filesCollection",
                    onSubmit    : that.onSubmit
                });

            _.bindAll(this, this.onSubmit); // attempting to bind 'this'
        },


        onSubmit: function(id, fileName){
            console.log(this); // still refers to 'fileUploader' object :(
        }

}

导致以下错误:

Uncaught TypeError: Cannot read property 'bind' of undefined

示例: http: //jsfiddle.net/WilsonPage/BE3Lp/5/

I am having issues attempting to preserve uploader as the this context when onSubmit is called. Can any JS gurus help out?

uploader: {

        init: function(){
            var that = this;

            var fileUploader = new Uploader.FileUploaderBasic({
                    button      : $("#upload-btn")[0],
                    action      : "/filesCollection",
                    onSubmit    : that.onSubmit
                });

            _.bindAll(this, this.onSubmit); // attempting to bind 'this'
        },


        onSubmit: function(id, fileName){
            console.log(this); // still refers to 'fileUploader' object :(
        }

}

Results in the following error:

Uncaught TypeError: Cannot read property 'bind' of undefined

Example: http://jsfiddle.net/WilsonPage/BE3Lp/5/

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

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

发布评论

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

评论(2

薆情海 2024-12-19 07:40:39

问题已解决: http://jsfiddle.net/WilsonPage/BE3Lp/41/

我发现的几件重要的事情:

  1. 必须在分配函数之前调用_.bindAll()
  2. 第一个参数必须是您想要绑定的对象或 this
  3. 后面的参数必须是对象中存在的函数的名称 (this),并且它们必须是字符串形式!
  4. 如果您想绑定对象中的所有函数(this) 然后省略任何函数名称并将对象 (this) 作为唯一的参数,例如。 _.bindAll(this)

希望这能帮助像我这样困惑的人!

Problem Solved: http://jsfiddle.net/WilsonPage/BE3Lp/41/

Several Important Things I Discovered:

  1. The _.bindAll() must be called before the function is assigned.
  2. The first argument must be the object or this you wish to bind.
  3. The arguments that follow must be the names of the functions present within the object (this) and they must be in string form!
  4. If you want to bind all functions in the object (this) then omit any function names and have the object (this) as the only argument eg. _.bindAll(this)

Hope this helps some confused peeps like me!

迷荒 2024-12-19 07:40:39

通过使用callapply>,您可以为任何函数指定this的上下文。这应该可以解决问题(在 fileUploader 声明中):

onSubmit: function() {
    that.onSubmit.apply(that, arguments);
}

编辑:更新了 jsfiddle:http: //jsfiddle.net/WDTBV/1/

By using call or apply you can specify the context for this for any function. This should do the trick (in the fileUploader declaration):

onSubmit: function() {
    that.onSubmit.apply(that, arguments);
}

Edit: Updated jsfiddle: http://jsfiddle.net/WDTBV/1/

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