[] .__ proto__ === Array.prototype // === [Symbol (Symbol.unscopables): Object]?

Defining a utility function for checking prototype object chains (in Chrome), I get this for arrays.

array prototype chain

So it seems that

[].__proto__ === Array.prototype // === [Symbol(Symbol.unscopables): Object] 

I understand the first equality. I have no idea what the third term is, although I heard that ES6 will have characters.

Is this thing the same as Array.prototype? Why is it printed this way?

Edit: chrome: // version information:

 Google Chrome 40.0.2214.111 (Official Build) Revision 6f7d3278c39ba2de437c55ae7e380c6b3641e94e-refs/branch-heads/ 2214@ {#480} OS Linux Blink 537.36 (@189455) JavaScript V8 3.30.33.16 
+7
javascript google-chrome prototype ecmascript-6
source share
1 answer

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

+2
source share

All Articles