簡體   English   中英

如何編寫JS函數foo(baz),其中baz是:function baz(para){alert(para.data);}

[英]How to write JS function foo(baz) where baz is: function baz(para){alert(para.data);}

我遇到了這段代碼; 我很好奇地嘗試編寫類似的代碼結構。 考慮地理定位

    navigator.geolocation.getCurrentPosition(getPosition);

    function getPosition(position) 
    {
      alert(position.coords.latitude);
    }

如何編寫類似getCurrentPosition的函數,該函數接受帶有參數getPosition(position)的函數並將對象分配給參數position以便可以讀取其屬性,例如position.coords.latitude

如何編寫類似的結構?
並且,可以編寫getPosition(position)使其返回,如下所示:

function getPosition(position) 
{
  return position.coords.latitude;
}

這就是我們所說的callback

 function funcOne(callback) { /* Do some stuff */ console.log('funcOne'); // Execute the callback callback(); } function funcTwo() { /* Do some more stuff */ console.log('funcTwo'); } funcOne(funcTwo); // Will execute both functions one after the other 
 <script src="http://www.wzvang.com/snippet/ignore_this_file.js"></script> 

至於第二個問題,是的,您可以在第一個函數中返回第二個函數的結果。 即使您從未顯式調用過它,它也會輸出funcTwo

 function funcOne(callback) { return callback(); // Execute the callback, return the result } function funcTwo() { return 'funcTwo'; } console.log( funcOne(funcTwo) ); // Outputs 'funcTwo' 
 <script src="http://www.wzvang.com/snippet/ignore_this_file.js"></script> 

console.log重寫由wZVanG提供

可以說我們正在創建一個API及其名為SuperAPI。 這就是我們定義它並公開一個名為getCurrentPosition的方法的方法,該方法給出一個位置。

var SuperAPI = function(){};
SuperAPI.prototype.getCurrentPosition = function(fn){ // <--- fn will be the function that needs to be executed and parameters need to be sent to it
  var pos = [10,0]; // <--- our API will do some magic and send position as a parameter to the function that is passed to getCurrentPosition
  fn.call(this, pos); // <--- this is responsible to invoke the function sent to getCurrentPosition and pass the parameters *currentPosition*
  return this; // <--- this will enable chaining
};
SuperAPI.prototype.someMoreStuff = function(){
  alert('some more stuff');
};

在此處檢查文檔以獲取有關致電的更多信息。 除調用外,apply還可以用於設置上下文和發送參數。

在客戶端,這就是我們消費它的方式。

var myapi = new SuperAPI();
myapi.getCurrentPosition(function(currentPosition){
  // currentPosition will be [10,0]
  alert(currentPosition[0] + ':' + currentPosition[1]); 
}).someMoreStuff();

 var SuperAPI = function() {}; SuperAPI.prototype.getCurrentPosition = function(fn) { var pos = [10, 0]; fn.call(this, pos); return this; }; SuperAPI.prototype.someMoreStuff = function() { alert('some more stuff'); }; var myapi = new SuperAPI(); myapi.getCurrentPosition(function(pos) { alert(pos[0] + ':' + pos[1]); }).someMoreStuff(); 

在此示例之后,您可以傳遞一個回調函數:

 var myObj = { bar: {x: 100, y:200}, foo: function(baz){ return baz(this.bar); } } myObj.foo(function(position){ console.log(position); }) 
 <script src="http://www.wzvang.com/snippet/ignore_this_file.js?theme=default"></script> 

這是一個例子:

function foo(callback) {
    var data = {};
    // here some code that fills the variable with some real values
    callback(data); // calls the passed function with the data object
};

foo(function(data) {
    console.log(data);
});

基本上,傳遞的函數在foo代碼中的某處被調用。

你基本上是問的回調函數模式,並有大量的來源,以了解他們 至於問題的最后一部分,從技術上講是可以的,但是如果它是異步操作,則不會分配給任何東西。

例如:

 function doSomethingSync(f) { return f('FOO'); } function doSomethingAsync(f) { setTimeout(function () { return f('BAR'); }, 0); } function returnVal(val) { return val; } var foo = doSomethingSync(returnVal); var bar = doSomethingAsync(returnVal); document.write('<pre>foo === ' + foo + '</pre><pre>bar === ' + bar + '</pre>'); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM