简体   繁体   English

javascript 回调不起作用

[英]javascript callback not working

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 receives the value, ie pageUrl is undefined, however it was correctly passed in first function.我观察到的是,第二个 function 没有接收到该值,即 pageUrl 未定义,但是它在第一个 function 中正确传递。 Any pointers?任何指针?

Your syntax to call is incorrect;call的语法不正确; the first parameter to call is what determines the value of this inside of the function you're calling.call的第一个参数是确定您正在调用的 function 内部的this的值。 The second argument is where you would place an array of arguments to pass to the function第二个参数是放置 arguments 数组以传递给 function 的位置

You could simply use你可以简单地使用

callback(tab.url);

in this case.在这种情况下。

If you wanted to use call :如果你想使用call

callback.call(this, tab.url);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM