9.5 Object.create(): Creating objects via descriptors

Object.create() creates a new object. Its first argument specifies the prototype of that object. Its optional second argument specifies descriptors for the properties of that object. In the next example, we create the same object as in the previous example.

  1. const legoBrick2 = Object.create(
  2. Object.prototype,
  3. {
  4. kind: {
  5. value: 'Plate 1x3',
  6. writable: true,
  7. enumerable: true,
  8. configurable: true,
  9. },
  10. color: {
  11. value: 'yellow',
  12. writable: true,
  13. enumerable: true,
  14. configurable: true,
  15. },
  16. description: {
  17. get: function () {
  18. return `${this.kind} (${this.color})`;
  19. },
  20. enumerable: true,
  21. configurable: true,
  22. },
  23. });
  24. // Did we really create the same object?
  25. assert.deepEqual(legoBrick1, legoBrick2); // Yes!