user944513
user944513

Reputation: 12729

Why does object.defineproperty not add the property to the object?

I am using es6 syntax. Why is property d added to object b, but when I am using Object.defineproperty, the property c is not added to the object b ?

Here is my code

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/

Upvotes: 1

Views: 674

Answers (1)

Paul
Paul

Reputation: 141829

Object.assign only copies enumerable own properties.

Object.defineProperty defines non enumerable properties unless the descriptor overrides the default value (false) for enumerable.

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)

Upvotes: 3

Related Questions