=>
返回 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許可證)數組