Javascript Scope problem with OAuth
I was wondering how to call waitForPin
in this line:
setTimeout(this.waitForPin, 100);
File:
// namespace our code
window.App = {};
// my class (no Class.create in JQuery so code in native JS)
App.Something = function() {
};
// add functions to the prototype
App.Something.prototype = {
// automatically called
initialize: function(name, sound) {
this.wnd;
this.config = {
// some vars
};
this.oauth = new OAuth(this.config);
// attach eve开发者_运维问答nt handlers, binding to 'this' object
$("#request_token").click($.proxy(this.request_token, this));
},
request_token: function() {
this.oauth.fetchRequestToken(this.openAuthoriseWindow, this.failureHandler);
},
openAuthoriseWindow: function (url) {
this.wnd = window.open(url, 'authorise');
setTimeout(this.waitForPin, 100); // how to call waitForPin here?
},
waitForPin : function (scope) {
// do sth
}
};
setTimeout(window.App.Something.waitForPin(scope), 100);
The waitForPin
function is attached to the prototype of Something
. In order to access it create an instance of Something
and call the method on that instance. Example
setTimeout((new App.Something()).waitForPin, 100);
setTimeout(this.waitForPin, 100);
That's the correct way to do it.
var Q = new App.Something;
Q.openAuthoriseWindow();
Demo: http://jsfiddle.net/bBRPv/
What about this:
setTimeout($.proxy(this.waitForPin, this), 100);
精彩评论