2017-02-16 76 views
1

我正在使用es6语法。为什么将属性d添加到对象b,但是当我使用Object.define属性时,属性c未添加到对象b为什么object.define属性不会将属性添加到对象中?

这里是我的代码

var a = { 
 
    a: 1 
 
} 
 
var b = { 
 
    a: 5, 
 
    b: 6 
 
} 
 

 
b['d'] = 33 
 
Object.defineProperty(b, 'c', { 
 
    value: 'eee' 
 
}) 
 

 
var t = {} 
 
Object.assign(t, a, b) 
 
console.log(t)

https://es6console.com/iz8m4ux1/

+0

有一个在你的问题没有ES6特定的语法。 – Paulpro

回答

2

Object.assign只复制枚举自己的属性。

Object.defineProperty限定非枚举的属性,除非描述符将覆盖enumerable默认值(false)。

var a ={a:1} 
 
var b={a:5,b:6} 
 
b['d']=33 
 
Object.defineProperty(b,'c',{ 
 
value:'eee', 
 
enumerable: true 
 
}) 
 
var t ={} 
 
Object.assign(t,a,b) 
 
console.log(t)

+0

什么是枚举?什么是这个属性的使用 – user944513

+0

@ user944513 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties – Bergi