Object.create method in javascript

你离开我真会死。 提交于 2019-12-01 17:03:11

The issue is that writable and set/get are mutually exclusive. The code generates this helpful error in Chrome:

Invalid property. A property cannot both have accessors and be writable...

This makes some logical sense: if you have set/get accessors on a property, that property is never going to be written to and/or read from, because any attempts to read/write it will be intercepted by the accessor functions. If you define a property as writable and give it accessor functions, you are simultaneously saying:

  1. "The value of this property can be directly altered," and
  2. "Block all attempts to read and/or write to this property; instead, use these functions."

The error is simply stopping you from specifying a contradiction. I assume from the fact that you wrote a getter and setter, you don't really want the property to be writable. Just remove that line, and your code runs perfectly.

Late answer, not looking for votes, but hoping this will be helpful.

There are two kinds of properties. Each property is EITHER:

  1. a data property which has these four attributes:

    • value
    • writable
    • enumerable
    • configurable
  2. OR an accessor property which has these four attributes:

    • get
    • set
    • enumerable
    • configurable

Therefore there is no property that can have both get and writable. That's just the way JavaScript is! Please see section 8.6 of the ECMAScript Standard for the gory details.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!