js 箭头函数 (state) => (id) => {}
在查看Vue官方文档时,看到如下js:
getters: {
// ...
getTodoById: (state) => (id) => {
return state.todos.find(todo => todo.id === id)
}
}
刚开始看这段代码(state) => (id) => {} 不知道是什么意思,但细想,id => {} 这本身是一个匿名函数,等同
function (id) {
// ...
}
那么 (state) => (id) => {} 是不是表示:
function (state) {
return function (id) {
return ...;
};
}
看下面一个例子:
var a = {
xx: 11,yy: (x) => (y) => {
return x.find(e => e == y)
}
};
console.log(a.yy([1, 2, 3])(1));
最后运行结果:

上面等价写法:
var a = {
xx: 11,
yy: function(x) {
return function(y) {
return x.find(e => e == y);
}
}
};
console.log(a.yy([1, 2, 3])(1));

本文来自博客园,作者:margo,转载请注明原文链接:https://www.cnblogs.com/ZMargo/articles/13141435.html

浙公网安备 33010602011771号