5

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_Objects/Object/create

In the example code, line 18. A accessor property is created with writable set to true. I also read that writable is only for data descriptors.

Tried running,

var o = Object.create(Object.prototype, {
  // foo is a regular "value property"
  foo: { 
    writable:true, configurable:true, value: "hello" 
  },
  // bar is a getter-and-setter (accessor) property
  bar: {
    writable: true,
    configurable: false,
    get: function() { return 10 },
    set: function(value) { console.log("Setting `o.bar` to", value) }
  }
  });
console.log(o); 

I get invalid property error.

2
  • is the mozilla reference incorrect? Commented Jul 27, 2012 at 5:42
  • The Mozilla reference was indeed incorrect. It's a wiki, so I just logged in and fixed it. (They're rolling out a new wiki very shortly, so you might not see the fix just yet, depending on whether you are enrolled in the beta, I think.) Commented Jul 27, 2012 at 5:52

2 Answers 2

12

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.

Sign up to request clarification or add additional context in comments.

2 Comments

+1. Just delete the writable: true bit from bar, and it's fine.
@John You might be interested in John Resigs blog post about ECMAScript 5: ejohn.org/blog/ecmascript-5-objects-and-properties
10

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.

1 Comment

This answer was very helpful for me. I was getting the same error, even though I had writable set to false. Now I understand, because I had defined a 'get', the property was an 'accessor property' and therefore it's illegal to set any value for 'writable' (even if you set it to false)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.