Why Object.create is not working in node.js

In the developer console (Mozilla, Chrome, nvm), this code works as expected:

var proto = {x: 3};
var obj = Object.create(proto);

So there objwill be{x: 3}

But in node.js I get {}
Why?

+4
source share
2 answers

Everything is working fine. However, the object {x:3}is a prototype obj. When Node prints an object, it only prints its own properties. x- property of the prototype. Give it a try!

var proto = {x: 3};
var obj = Object.create(proto);
alert(obj.x) // 3
Run codeHide result

(Yes, I know that this is a browser, but it is the same JavaScript. :))

More details: Object.create()

+5
source

Node.js, console.log , util.inspect . console.log doc,

, util.inspect.

util.inspect, , , showHidden, true, , .

, , . , util.inspect . ?

, , - for..in. for..in MDN doc,

, (, , ).

,

var proto = {
    x: 3
};
var obj = Object.create(proto);
for (var key in obj) {
    console.log(key);
}
// x

console.log ECMA, . , for..in , node .

+5

All Articles