jQuery'这个'在回调中

时间:2015-06-12 22:18:03

标签: filepicker.io

我的filepicker.io对话框很好,但在成功回拨的过程中我似乎失去了我的'这个'上下文。

所以我的代码就像这样

var fileProcess ={

 saveFileInfo: function () {
.....process info here
},

selectFile: function () {
        filepicker.pick({ mimetype: 'image/*' }, function (Blob) {
           this.saveFileInfo();
        });

    }

}

那里有类似" context:this"就像我可以在ajax电话中做的那样?

1 个答案:

答案 0 :(得分:0)

尝试创建一个名为selfme的新变量,设置为当前值this OUTSIDE您的回调。然后,您使用闭包,这样您就可以通过thisme从回调中访问self

喜欢:

this.mesg = "Hello";
var self = this;
function handler() {
  alert(self.mesg);
}
在上下文切换后

...

handler(); // Will alert 'Hello'
编辑:哦,坚果......只是意识到这样做不行......试试:

function handler() {
  alert(this.msg);
}
var newHandler = handler.bind(this);

...后来

newHandler();

Function.prototype.bind()获取一个对象,并返回一个函数。每当调用返回的函数时,传递给bind()的对象都将用作this