传播运营商问题的财产访问者(getters) [英] spread operator issues with property accessors (getters)

查看:90
本文介绍了传播运营商问题的财产访问者(getters)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解以下代码存在以下问题的原因 https:// jsfiddle。 net / q4w6e3n3 / 3 /

I'm having a hard time understanding why there are some issues with the following code https://jsfiddle.net/q4w6e3n3/3/

注意:所有示例均在Chrome版本52.0.2743.116中进行测试,以防万一/ p>

Note: All examples are tested in chrome version 52.0.2743.116 just in case this helps

const model = {
  someVal: 'some val',
};


const obs = {
  ...model,
  get accessor() {
    return this.someVal;
  },
}

// Expected: '>>> some val'
// Actual: '>>> undefined'
console.log('>>>', obs.accessor);

但以下类似的代码片段 https://jsfiddle.net/q4w6e3n3/5/

But the following similar snippet works https://jsfiddle.net/q4w6e3n3/5/

const model = {
  someVal: 'some val',
};


const obs = {
  get accessor() {
    return this.someVal;
  },
  ...model,
}

// Expected: '>>> some val'
// Actual: '>>> some val'
console.log('>>>', obs.accessor);

使用babel REPL我能看到 Object.assign
当我直接使用它而不是对象传播时,我得到相同的问题,如果放入模型
变量到最后而不是在开头。

using the babel REPL I was able to see that Object.assign is used if available in the transpiled code When I used it directly instead of the object spread I get the same issue, and also works if put the model variable to the end instead of at the beginning.

这是一个错误吗?还是打算行为?

Is this a bug? or is it intended behavior?

同样为什么以下代码片段也能正常工作?:

Also why does the following snippet work as well?:

const model = {
  someVal: 'some val',
};


const obs = {
  someVal: model.someVal,
  get accessor() {
    return this.someVal;
  },
}

// Expected: '>>> some val'
// Actual: '>>> some val'
console.log('>>>', obs.accessor);

https://jsfiddle.net/q4w6e3n3/6/

我希望它有同样的问题,但作为一个魅力是吸气剂这个关键字以某种方式绑定到它们被添加到的对象?

I would expect it to have the same issues but works as a charm are getters this keyword somehow bound to the object they were added to?

推荐答案

Object.assign不会复制访问者。因此,当你的splat在你的getter babel和它的各种voodoos之前使用Object.assign并且访问者没有被复制到第二个,那个好的,伏都教的第一个对象。当你的splat在getter之后,然后它使用getter将splatted属性赋值给对象,并保留getter。

Object.assign won't copy accessors. So when your splat is before your getter babel and its various voodoos uses Object.assign and the accessor isn't copied onto the first object from the second, well kind of, voodoo again. When your splat is after the getter, then it assigns the splatted properties onto the object with the getter, and the getter is preserved.

见这里: MDN - Object.assign

相关代码摘录:

**Copying Accessors**
var obj = {
  foo: 1,
  get bar() {
    return 2;
  }
};

var copy = Object.assign({}, obj); 
console.log(copy); 
// { foo: 1, bar: 2 }, the value of copy.bar is obj.bar's getter's return value.

// This is an assign function that copies full descriptors
function completeAssign(target, ...sources) {
  sources.forEach(source => {
    let descriptors = Object.keys(source).reduce((descriptors, key) => {
      descriptors[key] = Object.getOwnPropertyDescriptor(source, key);
      return descriptors;
    }, {});
    // by default, Object.assign copies enumerable Symbols too
    Object.getOwnPropertySymbols(source).forEach(sym => {
      let descriptor = Object.getOwnPropertyDescriptor(source, sym);
      if (descriptor.enumerable) {
        descriptors[sym] = descriptor;
      }
    });
    Object.defineProperties(target, descriptors);
  });
  return target;
}

var copy = completeAssign({}, obj);
console.log(copy);
// { foo:1, get bar() { return 2 } }

这篇关于传播运营商问题的财产访问者(getters)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆