javascript es6休息算子和箭头函数与三元算子实验

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript es6休息算子和箭头函数与三元算子实验相关的知识,希望对你有一定的参考价值。

/*eslint no-unused-vars:0 */

let alpha = ['a','b','c', {first: 'first'}];
let beta = ['be', 'ta', { first: 'second'}];

let more = {text:'more', date: new Date()};

const gamma = [...alpha, more];

console.log(gamma, alpha);

let todos = [{id: 1, completed: false}, {id:2, completed: true}];
console.log("initial todos", todos);
function doAction (action) {
    console.log("13 action", action);
    todos.map( (todo, index) => {
        console.log(todo, index);
        if(index === action.index) {
            console.log('17 inside if', todo);
            // todo.completed = !todo.completed;
            return Object.assign({}, todo, {
                completed: !todo.completed
            });
        }
        console.log('24', todo);
        return todo
    })
}
let action = { index: 2 };

/* trying that 

- no curly bracket needed when using ternary in new line using arrow function

- can debug only when run method in console
- original object does not change
*/
const nocurly = () => {
    return todos.map( todo =>
                (todo.id === action.index)
                ? Object.assign({}, todo, { completed: !todo.completed }, { newprop:'trying more without curly'} )
                : todo
        );
};

console.log('nocurly called', nocurly());

setTimeout( () => console.log('final todos', todos), 2000);

以上是关于javascript es6休息算子和箭头函数与三元算子实验的主要内容,如果未能解决你的问题,请参考以下文章

javascript JavaScript箭头函数 - ES6

es6 箭头函数和function的区别

JavaScript ES6 箭头函数 匿名函数 普通函数

为啥我不能在 JavaScript/ES6 中使用带有箭头函数的`new`? [复制]

javascript箭头函数

javascript基础修炼——指向FP世界的箭头函数