Search code examples
javascriptecmascript-6ecmascript-5

Object.defineProperty in Node.js


I am running the following code in a browser console and also with node.js v9.11.1 in the terminal:

let name = {};
Object.defineProperty(name, 'last', {value: 'Doe'});
console.log(name);

The browser console works properly and outputs { last: 'Doe' }. But in the terminal with node.js, it fails and outputs a blank object, {}.

What could be the issue here?


Solution

  • One of the properties of property descriptors is enumerable, which has the default value false. If a property is non-enumerable, Node.js chooses not to display the property, thats it.

    You can change that bit and try this

    let name = {};
    Object.defineProperty(name, 'last', {
      value: 'Doe',
      enumerable: true
    });
    console.log(name);