0

On my attempt to learn more about Object.create I came across Object.create(): the New Way to Create Objects in JavaScript.

An example from the above page:

var Car2 = Object.create(null); //this is an empty object, like {}
Car2.prototype = {
  getInfo: function() {
    return 'A ' + this.color + ' ' + this.desc + '.';
  }
};

var car2 = Object.create(Car2.prototype, {
  //value properties
  color:   { writable: true,  configurable:true, value: 'red' },
  //concrete desc value
  rawDesc: { writable: false, configurable:true, value: 'Porsche boxter' },
  // data properties (assigned using getters and setters)
  desc: { 
    configurable:true, 
    get: function ()      { return this.rawDesc.toUpperCase();  },
    set: function (value) { this.rawDesc = value.toLowerCase(); }  
  }
}); 
car2.color = 'blue';
alert(car2.getInfo()); //displays 'A blue PORSCHE BOXTER.'

Question:

  1. How correct is the above example? This answer seems to contradict the example above.It seems to give the notion that rawDesc could be a private member that could be modified only via getter/setter of desc. Is this useful in any way?

  2. Also, trying to set value for desc using car2.desc = 'Merc' doesn't seem to work. Why is that so?

  3. What parts of Object.defineProperty and Object.create are similar?

Research:

Somewhat related question: Why can I set [enumerability and] writability of unconfigurable property descriptors? I have tried removing writable: false and value: 'Porsche boxter' and tried setting the value but to no avail.

Community
  • 1
  • 1
Siva
  • 186
  • 1
  • 10

1 Answers1

0

Some comments:

var Car2 = Object.create(null); //this is an empty object, like {}

The comment isn't quite correct. The internal prototype of Car2 (i.e. its [[Prototype]]) will be null, so it doesn't inherit any properties of Object.prototype, whereas an object created using {} does.

Car2.prototype = {
  ...
};

var car2 = Object.create(Car2.prototype, {

It seems pointless to create Car2 since it isn't a function and can't be used as a constructor nor has it inherited any of the standard Object methods. It's just a placeholder for the object assigned to Car2.prototype. But I guess this is just an example.

And for the questions…

How correct is the above example? This answer seems to contradict the example above.

How does it contradict the example?

Also, trying to set value for desc using car2.desc = 'Merc' doesn't seem to work. Why is that so?

Because the setter for desc actually changes rawDesc, but rawDesc is set to writeable: false. Change it to writeable: true and the value is changed. But it's a public property anyway so a bit pointless to have its value set by setting some other property.

What parts of Object.defineProperty and Object.create are similar?

Object.create and Object.defineProperty are adjacent in ECMA–262, not hard to work it out. Basically, the first just adds a property to an existing object, the second creates a new object and sets its [[Prototype]] (i.e. it's equivalent to a constructor).

RobG
  • 142,382
  • 31
  • 172
  • 209
  • Thanks and it's indeed an copy-pasted example, as mentioned in the question. As for the example contradicting the answer, I guess I misconstrued `rawDesc` and desc being the same property. Just that on first glance, the example looks to be providing a way to have private members ie., `rawDesc` in this case, which can be accessed only via `desc`. Btw @RobG any chance your are the same person (Rob Gravelle) who wrote the article with this same example [here](http://www.htmlgoodies.com/beyond/javascript/object.create-the-new-way-to-create-objects-in-javascript.html) ? – Siva Mar 21 '13 at 10:16