We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
写在前面: ES6中对象的属性名都是字符串,容易造成重名,污染环境。
注意:Symbol函数前不能使用new命令,否则会报错。这是因为生成的 Symbol 是一个原始类型的值,不是对象。也就是说,由于 Symbol 值不是对象,所以不能添加属性。基本上,它是一种类似于字符串的数据类型。
let symbol = Symbol(); let obj = {}; obj[symbol] = 'hello';
let symbol = Symbol('one'); let symbol2 = Symbol('two'); console.log(symbol);// Symbol('one') console.log(symbol2);// Symbol('two')
// 当使用 for...of 去遍历某个数据结构的时候,首先去找 Symbol.iterator 接口 // 若 找到了就去遍历,否则返回 TypeErr: xxx is not iterator const data = { 0: 'Tom', 1: 18, length: 2, [Symbol.iterator]: function() { let nextIndex = 0 return { next: function() { return nextIndex < this.length ? { value: this[nextIndex++], done: false } : { value: undefined, done: true } } } } }
参考资料:
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Symbol 的特点:
注意:Symbol函数前不能使用new命令,否则会报错。这是因为生成的 Symbol 是一个原始类型的值,不是对象。也就是说,由于 Symbol 值不是对象,所以不能添加属性。基本上,它是一种类似于字符串的数据类型。
Symbol 的使用
参考资料:
The text was updated successfully, but these errors were encountered: