1

As you can see in the code below, both of these "get" functions in manually-created objects have identical implementations - so why do they produce a different answer?

animal = Object.create({})

Object.assign(animal, {
  _fur: 'This might not be furry',
  get fur() {
    return this._fur
  },
  fur_get: function () {
    return this._fur
  }
})

cat = Object.create(animal)

Object.assign(cat, {
  _fur: 'This is very furry'
})

console.log(cat.fur)
console.log(cat.fur_get())

Output:

This might not be furry
This is very furry
  • 1
    `Object.assign` doesn't copy getters and setters, instead it evaluates them when accessing the property. – Bergi Aug 08 '20 at 22:18
  • 1
    Just use `animal = Object.create({}, {fur: {get() { return this._fur }, enumerable: true})` – Bergi Aug 08 '20 at 22:24

0 Answers0