callback()的定义在哪里?

时间:2017-06-22 08:42:09

标签: javascript google-chrome-extension callback

以下是Chrome扩展程序初始页示例的摘要。https://developer.chrome.com/extensions/getstarted

我想知道最后调用的callback()函数的主体在哪里。

/**                                                                                  
  6  * Get the current URL.                                                              
  7  *                                                                                   
  8  * @param {function(string)} callback - called when the URL of the current tab       
  9  *   is found.                                                                       
 10  */                                                                                  
 11 function getCurrentTabUrl(callback) {                                                
 12   // Query filter to be passed to chrome.tabs.query - see                            
 13   // https://developer.chrome.com/extensions/tabs#method-query                       
 14   var queryInfo = {                                                                  
 15     active: true,                                                                    
 16     currentWindow: true                                                              
 17   };                                                                                 
 18                                                                                      
 19   chrome.tabs.query(queryInfo, function(tabs) {                                      
 20     // chrome.tabs.query invokes the callback with a list of tabs that match the     
 21     // query. When the popup is opened, there is certainly a window and at least     
 22     // one tab, so we can safely assume that |tabs| is a non-empty array.            
 23     // A window can only have one active tab at a time, so the array consists of     
 24     // exactly one tab.                                                              
 25     var tab = tabs[0];                                                               
 26                                                                                      
 27     // A tab is a plain object that provides information about the tab.              
 28     // See https://developer.chrome.com/extensions/tabs#type-Tab                     
 29     var url = tab.url;                                                               
 30                                                                                      
 31     // tab.url is only available if the "activeTab" permission is declared.          
 32     // If you want to see the URL of other tabs (e.g. after removing active:true     
 33     // from |queryInfo|), then the "tabs" permission is required to see their        
 34     // "url" properties.                                                             
 35     console.assert(typeof url == 'string', 'tab.url should be a string');            
 36                                                                                      
 37     callback(url);                                                                   
 38   });                                                                                
 39    

2 个答案:

答案 0 :(得分:0)

变量在这里的参数中定义:

function getCurrentTabUrl(callback) {

该功能未在您引用的代码中定义。它在别处定义,然后在调用getCurrentTabUrl时传递。

e.g。

function foo() { /* do something */ }
getCurrentTabUrl(foo);

答案 1 :(得分:0)

它会像:

getCurrentTabUrl(function(url){
alert(url);
});

注意调用getCurrentTabUrl的地方!