Comment to 2014-04-14
Chrome console.log seems to print all the keys of type Symbol, here is an example:
var o = {}; o[Symbol.iterator] = function () {}; o[Symbol.unscopables] = {}; var s = Symbol('s'); o[s] = 3; console.log(o);
which prints:
Object {Symbol (Symbol.unscopables): Object, Symbol (Symbol.iterator): function, Symbol (s): 3}
I donβt understand why chrome behaves this way, is it for debugging or something else?
Fortunately, this does not affect the result of toString() , so all code is safe.
It seems that console.log will especially print the Symbol.unscopable key in the console, we can have a simple object to execute as follows:
var o = {}; o[Symbol.unscopables] = {}; console.log(o);
which outputs:
Object {Symbol (Symbol.unscopables): Object}
The Symbol.unscopables symbol is a special symbol defined in ES6 as @@unscopables , which is used to exclude certain properties when this object works in the with environment, an official explanation:
An object-valued property whose property names are property names that are excluded from the environment bindings with the associated object.
A simple example:
var o = {x: 1, y: 2}; o[Symbol.unscopables] = { y: true }; console.log(ox); // 1 console.log(oy); // 2 with(o) { console.log(x); // 1 console.log(y); // ReferenceError: y is not defined }
You can use Array.prototype[Symbol.unscopables] to find all keys that cannot be used with
However, the output of Array.prototype not quite the same as a regular object with the Symbol.unscopables key; it outputs [Symbol(Symbol.unscopables): Object] , which is a format more like an array
I canβt explain exactly why this could be due to the Symbol.toStringTag , which controls how the object should be formatted for the string, but Chrome does not currently export this symbol, so itβs hard to check