Object.create method in javascript

后端 未结 2 1142
你的背包
你的背包 2021-01-18 09:09

Being a beginner in javascript, i tried to understand Object.create() method from here

https://developer-new.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objec

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-18 09:46

    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.

提交回复
热议问题