I'm curious about the way Node.js prints objects through console.log(object).
I have the following code (from Learning Javascript Design Patterns book) under a file constructor.js
var defineProp = function(obj, key, value){
var config = {
value: value,
writable: true,
configurable: true
};
Object.defineProperty(obj, key, config );
}
var person = Object.create(Object.prototype);
defineProp(person, "car", "Delorean");
defineProp(person, "dateOfBirth", "1981");
defineProp(person, "hasBeard", false);
console.log(person); //This prints {} in Node.js
Running with that code with >node constructor.js prints an empty object. Chrome however, prints what I would expect if I run the code inside an HTML file.
console.log(person); //Chrome prints Object {car: "Delorean", dateOfBirth: "1981", hasBeard: false}
Note: I can still print the attributes (such as console.log(person.car)) under Node, just not the object itself (such as console.log(person))
Why is this? Do Chrome and Node use separate prototypes for the console object, even though they share the same javascript engine?
enumerable: trueto theconfigobject the properties will print in node.js.defineProperty()are not enumerable. So, perhaps node.js is only showing you enumerable properties and Chrome shows non-enumerable properties.console.log()is not a something specified in a standard and it is a host object, not official part of the Javascript language so different environments may have different behaviors.console.log()is only synchronous when stdout is redirected to a file. Otherwise it is always asynchronous on all platforms. There are already several issues on node's github issue tracker about that.