1
0
mirror of https://github.com/ruanyf/es6tutorial.git synced 2025-05-24 18:32:22 +00:00

docs(Symbol): edit Symbol.unscopables

This commit is contained in:
Ruan YiFeng 2017-01-25 09:24:20 +08:00 committed by GitHub
parent 4b17bc8a16
commit dcb12a1be9

View File

@ -807,18 +807,19 @@ Array.prototype[Symbol.unscopables]
// entries: true, // entries: true,
// fill: true, // fill: true,
// find: true, // find: true,
// findIndex: true, //   findIndex: true,
// includes: true,
// keys: true // keys: true
// } // }
Object.keys(Array.prototype[Symbol.unscopables]) Object.keys(Array.prototype[Symbol.unscopables])
// ['copyWithin', 'entries', 'fill', 'find', 'findIndex', 'keys'] // ['copyWithin', 'entries', 'fill', 'find', 'findIndex', 'includes', 'keys']
``` ```
上面代码说明,数组有6个属性会被with命令排除。 上面代码说明,数组有7个属性会被`with`命令排除。
```javascript ```javascript
// 没有unscopables时 // 没有 unscopables
class MyClass { class MyClass {
foo() { return 1; } foo() { return 1; }
} }
@ -829,7 +830,7 @@ with (MyClass.prototype) {
foo(); // 1 foo(); // 1
} }
// 有unscopables时 // 有 unscopables
class MyClass { class MyClass {
foo() { return 1; } foo() { return 1; }
get [Symbol.unscopables]() { get [Symbol.unscopables]() {
@ -844,3 +845,4 @@ with (MyClass.prototype) {
} }
``` ```
上面代码通过指定`Symbol.unscopables`属性,使得`with`语法块不会在当前作用域寻找`foo`属性,即`foo`将指向外层作用域的变量。