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);
})
它只是为回调函数设置一个默认值。如果函数调用时未传递回调函数,则使用默认回调函数。看见