2015-04-21 26 views
-3

我需要一個例子。數據如何從URL /路徑獲取,以及它如何將數據作爲參數傳遞給匿名函數,代碼如何執行我們在匿名函數中編寫的任何內容。

得到具有步驟

+0

爲什麼不讀取'$ .get'的jquery源代碼? – atmd

+0

首先閱讀關於xmlhttprequest。 http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp – SSA

+1

這是功課嗎? – DelightedD0D

回答

1

這就是所謂的回調函數。它作爲參數傳遞給$ .get函數,並且在特定參數的情況下從該函數內部調用。看下面的例子:

function test(param1, callbackFunction) { 
    if(param1) { 
     var a = 1, b = 2; 
     callbackFunction(a, b); // this is a call on the callbackFunction method received as parameter 
    } 
} 

test(true, function(x, y) { 
    console.log(x, y); // 1,2 
}); 
+0

是的,我真的很想這樣。 – Rahaman

1

函數可以作爲參數被傳遞的示例,並且接收功能可以通過使用()參數名之後執行它。例如:

function process(callback){ 
    // do something - could be async or not, doesn't matter, then execute the callback, passing arguments 
    callback('something', 1, false); 
} 

process(function(firstArg, secondArg, thirdArg){ 
    console.log("called"); 
    console.log(firstArg); 
    console.log(secondArg); 
    console.log(thirdArg); 
}); 

抓取從URL數據通過XHR完成,但它並不重要的功能是幹什麼的,經過回調和執行他們的基本過程是相同的。