简体   繁体   English

javascript中的回调函数从何处获取其参数?

[英]Where does a callback function in javascript get its arguments from?

I am able to run a code like this 我可以运行这样的代码

myObject.save(function(error, data){
    if(error){
      console.log(error);
    }
    else {
      console.log(data);
    }
});

What i don't understand is where the anonymous function get the arguments error and data from? 我不明白的是匿名函数从哪里获取参数错误和数据?

Its from the thing that calls it - so save might look like this: 它来自调用它的东西-因此保存可能如下所示:

    function save(callback) {
        //do stuff

        var error = false;
        var data = {
            something: 'horse'
        };

        callback(error, data)
    }

There's no magic going on - its just a function call 没有魔术在发生-它只是一个函数调用

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

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