Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ES6回调函数_Javascript_Ecmascript 6_Callback - Fatal编程技术网

Javascript ES6回调函数

Javascript ES6回调函数,javascript,ecmascript-6,callback,Javascript,Ecmascript 6,Callback,我正在浏览其他人的代码,并不断看到以这种风格编写的函数: getConsents: (_, callback = () => {}) => { const data = {}; callback(data, true); } 我知道有些人在参数不合适时使用下划线作为跳过参数的约定,尽管我无法理解回调函数参数为何以这种方式编写 我尝试使用babel,看看它在es5中是否有意义,但运气不好: getConsents: (function

我正在浏览其他人的代码,并不断看到以这种风格编写的函数:

    getConsents: (_, callback = () => {}) => {
        const data = {};
        callback(data, true);
    }
我知道有些人在参数不合适时使用下划线作为跳过参数的约定,尽管我无法理解回调函数参数为何以这种方式编写

我尝试使用babel,看看它在es5中是否有意义,但运气不好:

getConsents: (function (_) {
  var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {};
  var data = {};
  callback(data, true);
});

如果有人能解释一下这个公约,或者详细说明它在做什么,我们将不胜感激

自ES6以来,您可以为函数参数指定默认值。例如:

函数greetname='johndoe'{ console.log'hello',name; } 问候“艾伦·阿尔达”;
问候 在ES5中,它可能看起来像:

getConsents: (function(_, callback = function() {}) {
    const data = {};
    callback(data, true);
})

它只是为回调函数设置一个默认值。

如果函数调用时未传递回调函数,则使用默认回调函数。看见