简体   繁体   中英

How to access the Symbol(Symbol.toStringTag) property?

I'm looking to read the data from the Symbol(Symbol.toStringTag) field of an object - in this case, it should return "Array Iterator".

在此处输入图片说明

This does not work

a.__proto__["Symbol(Symbol.toStringTag)"]

Symbol(Symbol.toStringTag) indicates that the property is a symbol . Symbols are unique values and you can only reference such a property if you already have a reference to the symbol.

Luckily Symbol.toStringTag is a well-known symbol , so you can just reference it:

a[Symbol.toStringTag]

Here is an example where you cannot access the property directly:

var obj = (function() {
  // prop is a unique value
  var prop = Symbol();
  return {[prop]: 42};
}());

There is no way to access obj[<the symbol>] directly since we don't have access to prop . You can still iterate over the properties though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM