=>
返回 map
对象// 通常的写法 const makeMap = () => { return {key: 'value'}; }; // 简洁的写法 const makeMap = () => ({key: 'value'});
// 通常的写法 const makeMap = (key, value) => { const obj = {}; obj[key] = value; return obj; }; // 简洁的写法 const makeMap = (key, value) => ({[key]: value});
const source = {hello: 'hello', hi: 'hi'}; // 通常的写法 const target = Object.assign({}, source); target.hello = 'hello everyone'; // 简洁的写法 const target = {...source, hello: 'hello everyone'};
const arr = [1, 2, 3]; const plus = (...args) => args.reduce((a, b) => a + b); // 通常的写法 plus(arr[0], arr[1], arr[2], 4, 5); // 简洁的写法 plus(...arr, 4, 5);
const arr1 = [1, 2, 3]; const arr2 = [4, 5, 6]; // 通常的写法 arr1 = arr1.concat(arr2); // 简洁的写法 arr1.push(...arr2);
// 通常的写法 promise.catch(e => { console.log(e); }); // 简洁的写法 promise.catch(console.log);
=>
// 通常的写法 const makeTimesFunc = times => { return value => { return value * times; }; }; // 简洁的写法 const makeTimesFunc = times => value => value * times;
// 不肯定元素个数,举例 3 个 const fnCollection = [str => `${str} | fisrt`, str => `${str} | second`, str => `${str} | third`]; // 通常的写法 const addManySuffixes = str => { let result = str; for(let i = fnCollection.length - 1; i > -1; i -= 1) result = fnCollection[i](result); return result; }; // 简洁的写法 const addManySuffixes = fnCollection.reduce((a, b) => str => a(b(str))); // 能够把 str 参数扩展成任意参数 const addManySuffixes = fnCollection.reduce((a, b) => (...args) => a(b(...args)));
更多博客,查看 https://github.com/senntyou/blogsgit
做者:深予之 (@senntyou)github
版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)数组