开发者

javascript callback not working

开发者 https://www.devze.com 2023-03-10 06:12 出处:网络
I have a code like this function SocialMiner() { var verbose=true; var profileArray=new Array(); var tabUrl;

I have a code like this

function SocialMiner()     
{
    var verbose=true;
    var profileArray=new Array();
    var tabUrl;
    this.getTabUrl=function(callback)
    {
        chrome.tabs.getSelected(null, function(tab)
        {
            myUrl = tab.url;
            console.log("0"+tab.url);
            console.log("calling callback");
            callback.call(tab.url);

        });
    }    

    this.setTabUrlValue=function(pageUrl)
    {
        console.log("1"+pageUrl);
        tabUrl=pageUrl;
    }
};

I call the first method with second as callback

 var pageUrl=miner.getTabUrl(miner.setTabUrlValue);

What I observe is that , second function does not recei开发者_高级运维ves the value, i.e. pageUrl is undefined, however it was correctly passed in first function. Any pointers ?


Your syntax to call is incorrect; the first parameter to call is what determines the value of this inside of the function you're calling. The second argument is where you would place an array of arguments to pass to the function

You could simply use

callback(tab.url);

in this case.

If you wanted to use call:

callback.call(this, tab.url);
0

精彩评论

暂无评论...
验证码 换一张
取 消