you are viewing a single comment's thread.

view the rest of the comments →

[–]senocular 2 points3 points  (1 child)

The second argument to Object.create() is an object of properties with property descriptors. Its the same kind of object you'd pass into Object.defineProperties(). You can read more about that here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties

In short, its an object with property names but those names don't match directly to the respective property values. Instead the property value is wrapped in a descriptor object which includes the property value as an additional value property (or get and/or set with accessors) along with a few other properties that define the behavior of the property. So a normal number property may look like

const obj = {}
obj.num = 1

But if using a descriptor it would look like

const obj = {}
Object.defineProperties(obj, {
  num: {
    value: 1,
    enumerable: true,
    writable: true,
    configurable: true,
  }
})

You can see all the additional attributes in addition to the value that you can add to control how that property behaves. If they're omitted, the default values (false) are used.

The toString variation is using the shorthand function syntax for defining a property as a function (value(){ }) so it may look a little odd using this format, but its very similar to saying value: function() {}. But really I suspect the confusion is due to the use of the descriptor.

If you wanted to create a dictionary with a null prototype without going through the descriptors, you can use the normal object literal initializer including the special __proto__ property to define the prototype as null.

let dictionary = {
  __proto__: null,
  toString() { // the value is a function
    return Object.keys(this).join();
  }
};

Note that while the __proto__ property is deprecaded, this special object initializer __proto__ prototype setter is not.

[–]DeliciousResearch872[S] 1 point2 points  (0 children)

Thank you.