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
// bad const TYPE_AUDIO = 'AUDIO' const TYPE_VIDEO = 'VIDEO' const TYPE_IMAGE = 'IMAGE' // good const TYPE_AUDIO = Symbol(‘AUDIO’) const TYPE_VIDEO = Symbol(‘VIDEO’) const TYPE_IMAGE = Symbol(‘IMAGE’) function handleFileResource(resource) { switch(resource.type) { case TYPE_AUDIO: playAudio(resource) break case TYPE_VIDEO: playVideo(resource) break case TYPE_IMAGE: previewImage(resource) break default: throw new Error('Unknown type of resource') } }
const Example = (function() { var _private = Symbol('private'); class Example { constructor() { this[_private] = 'private'; } getName() { return this[_private]; } } return Example; })(); var ex = new Example(); console.log(ex.getName()); // private console.log(ex.name); // undefined
实例化的set和map是类数组,需要...或者Array.from()将其还原为数组
[...new Set(array)]
// bad function test(color) { switch (color) { case 'red': return ['apple', 'strawberry']; case 'yellow': return ['banana', 'pineapple']; case 'purple': return ['grape', 'plum']; default: return []; } } test('yellow'); // ['banana', 'pineapple'] // good const fruitColor = { red: ['apple', 'strawberry'], yellow: ['banana', 'pineapple'], purple: ['grape', 'plum'] }; function test(color) { return fruitColor[color] || []; } // better const fruitColor = new Map() .set('red', ['apple', 'strawberry']) .set('yellow', ['banana', pineapple]) .set('purple', ['grape', 'plum']); function test(color) { return fruitColor.get(color) || [] }
可遍历: Array, Set, Map, arguments, NodeList, Generator, String
ES2015 引入了 for..of 循环,它结合了 forEach 的简洁性和中断循环的能力 [for...in break或者continue对遍历无影响,当有return时会报错!](https://juejin.im/entry/5884717a1b69e6005919f0d3)
[for...in break或者continue对遍历无影响,当有return时会报错!](https://juejin.im/entry/5884717a1b69e6005919f0d3)
const map = new Map([['Li', 23], ['Zhang', 18]]) // {"Li" => 23, "Zhang" => 18} // 遍历key for (let key of map.keys()) { // ... } // 遍历value for (let key of map.values()) { // ... } // 遍历key和value for (let [key, value] of map.entries()) { // ... }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Symbol
Set(一维存key) 和 Map(二维存key和value)
for of
ES2015 引入了 for..of 循环,它结合了 forEach 的简洁性和中断循环的能力
[for...in break或者continue对遍历无影响,当有return时会报错!](https://juejin.im/entry/5884717a1b69e6005919f0d3)
The text was updated successfully, but these errors were encountered: