简体   繁体   English

Node.js中的Object.defineProperty

[英]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: 我正在浏览器控制台中运行以下代码,并且还在终端中运行node.js v9.11.1

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

The browser console works properly and outputs { last: 'Doe' } . 浏览器控制台正常工作,并输出{ last: 'Doe' } But in the terminal with node.js , it fails and outputs a blank object, {} . 但是在带有node.js的终端中,它失败并输出一个空白对象{}

What could be the issue here? 这里可能是什么问题?

One of the properties of property descriptors is enumerable , which has the default value false . 属性描述符的属性之一是enumerable ,其默认值为false If a property is non-enumerable, Node.js chooses not to display the property, thats it. 如果属性不可枚举,则Node.js选择不显示该属性,仅显示该属性。

You can change that bit and try this 您可以更改该位并尝试此操作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM