如何编写JS函数foo(baz),其中baz是:function baz(para){alert(para.data);}

时间:2015-06-29 22:57:55

标签: javascript

我遇到了这段代码;我奇怪地试图写一个类似的代码结构。我采取地理位置

    navigator.geolocation.getCurrentPosition(getPosition);

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

如何编写一些函数,如getCurrentPosition接受带参数getPosition(position)的函数,并将对象分配给参数position,以便可以读取其属性,如position.coords.latitude 1}}

如何写出类似的结构?
并且,是否可以编写getPosition(position)以便它返回如下:

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

5 个答案:

答案 0 :(得分:1)

我们称之为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>
&#13;
&#13;
&#13;

至于你的第二个问题,是的,你可以在第一个问题中返回第二个函数的结果。即使您从未明确调用它,也会输出funcTwo

&#13;
&#13;
function funcOne(callback) {
    return callback(); // Execute the callback, return the result
}

function funcTwo() {
    return 'funcTwo';
}

console.log( funcOne(funcTwo) ); // Outputs 'funcTwo'
&#13;
<script src="http://www.wzvang.com/snippet/ignore_this_file.js"></script>
&#13;
&#13;
&#13;

console.log覆盖是wZVanG的礼貌。

答案 1 :(得分:1)

让我们说我们正在创建一个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');
};

检查docs here以获取有关通话的更多信息。除了调用,apply也可用于设置上下文和发送参数。

在客户端,这就是我们消费它的方式。

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

&#13;
&#13;
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();
&#13;
&#13;
&#13;

答案 2 :(得分:1)

按照此示例,您可以传递回调函数:

&#13;
&#13;
var myObj = {
  bar: {x: 100, y:200},
  foo: function(baz){
    return baz(this.bar);
  }
}

myObj.foo(function(position){
  console.log(position);
})
&#13;
<script src="http://www.wzvang.com/snippet/ignore_this_file.js?theme=default"></script>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

以下是一个例子:

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的代码中的某处被调用。

答案 4 :(得分:0)

您基本上是在询问回调函数模式,并且有很多来源可以阅读them。至于问题的最后一部分,技术上但如果它是异步操作,它将不会被分配给任何东西。

例如:

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>');