Sunday 11 June 2017

node.js print object with additional [ Number ] field, like { [Number: 10] name: 'Dime', value: 10 }

I have read JavaScript the Definitive Guide, here is the enumeration class of chapter 9.6.2:

function inherit(p) {
    if (p == null) throw TypeError(); // p must be a non-null object
    if (Object.create) // If Object.create() is defined...
        return Object.create(p); // then just use it.
    var t = typeof p; // Otherwise do some more type checking
    if (t !== "object" && t !== "function") throw TypeError();

    function f() {}; // Define a dummy constructor function.
    f.prototype = p; // Set its prototype property to p.
    return new f(); // Use f() to create an "heir" of p.
}

function enumeration(namesToValues) {
    // This is the dummy constructor function that will be the return value.
    var enumeration = function() { throw "Can't Instantiate Enumerations"; };

    // Enumerated values inherit from this object.
    var proto = enumeration.prototype = {
        constructor: enumeration,                   // Identify type
        toString: function() { return this.name; }, // Return name
        valueOf: function() { return this.value; }, // Return value
        toJSON: function() { return this.name; }    // For serialization
    };

    enumeration.values = [];        // An array of the enumerated value objects

    // Now create the instances of this new type.
    for (var name in namesToValues) {       // For each value
        var e = inherit(proto);             // Create an object to represent it
        e.name = name;                      // Give it a name
        e.value = namesToValues[name];      // And a value
        enumeration[name] = e;              // Make it a property of constructor
        enumeration.values.push(e);         // And store in the values array
    }
    // A class method for iterating the instances of the class
    enumeration.foreach = function(f, c) {
        for (var i = 0; i < this.values.length; i++) f.call(c, this.values[i]);
    };

    // Return the constructor that identifies the new type
    return enumeration;
}
var Coin = enumeration({ Penny: 1, Nickel: 5, Dime: 10, Quarter: 25 });
var c = Coin.Dime;                      // This is an instance of the new class
console.log(c);  // => { [Number: 10] name: 'Dime', value: 10 }

// Construct other object with `name` and `value`.
var o = Object.create({});
o.name = 'sam';
o.value = 10;
console.log(o);  // => { name: 'sam', value: 10 }

When I run it on node, the output of the code is { [Number: 10] name: 'Dime', value: 10 }. But when I construct an object o with Object.create({}) and . syntax like the enumeration object. And then print it. The result is { name: 'sam', value: 10 }.

Why in node it print additional [Number: 10]? But in browser console, it won't.
My node version is v4.2.6 and linux.



via zhenguoli

No comments:

Post a Comment