与OAuth的Javascript范围问题

时间:2011-09-26 19:45:01

标签: javascript oauth

我想知道如何在这一行中调用waitForPin

setTimeout(this.waitForPin, 100);

文件:

// 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 event 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
    }

};

4 个答案:

答案 0 :(得分:0)

setTimeout(window.App.Something.waitForPin(scope), 100);

答案 1 :(得分:0)

waitForPin函数附加到Something的原型。为了访问它,创建一个Something的实例并在该实例上调用该方法。实施例

setTimeout((new App.Something()).waitForPin, 100);

答案 2 :(得分:0)

setTimeout(this.waitForPin, 100);

这是正确的方法。

var Q = new App.Something;
Q.openAuthoriseWindow();

演示:http://jsfiddle.net/bBRPv/

答案 3 :(得分:0)

这个怎么样:

setTimeout($.proxy(this.waitForPin, this), 100);