ES6 Structuring Assignment?

前端 未结 2 546
广开言路
广开言路 2020-12-29 08:58

The new destructuring assignment features of ES6 are fairly well known now (live copy on Babel\'s REPL); in the case of variables that already exist:



        
2条回答
  •  醉酒成梦
    2020-12-29 09:39

    Some experimental stuff, building on top of your answer.

    If you wanted to get a little cheeky you could emulate the assignment portion of it with a setter. Definitely not practical, but it's a fun way to see what the behaviour might look like on the outside, if maybe you could empty assign o[] =. (Babel)

    let a = '1', b = '2';
    let o = {z: '26'};
    
    Object.defineProperty(Object.prototype, '', {
      set: function (o) {
        Object.assign(this, o);
      }, configurable: true
    });
    
    o[''] = {a, b};
    

    Same issues you face with your answer, actually more, but some food for thought.

提交回复
热议问题