1
0
mirror of https://github.com/ruanyf/es6tutorial.git synced 2025-05-28 21:32:20 +00:00

Updated example and explanation of Symbol.species

This commit is contained in:
Jing Ma 2017-11-24 16:17:31 +08:00 committed by GitHub
parent d808c68ce0
commit 3f28d7b06e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -591,11 +591,14 @@ class MyArray extends Array {
let a = new MyArray(1,2,3);
let mapped = a.map(x => x * x);
a instanceof MyArray // true
a instanceof Array // true
mapped instanceof MyArray // false
mapped instanceof Array // true
```
上面代码中,由于构造函数被替换成了`Array`。所以,`mapped`对象不是`MyArray`的实例,而是`Array`的实例
上面代码中,`a``MyArray`的实例,所以`a instanceof MyArray`返回`true`。由于构造函数被替换成了`Array`,所以`a`实际上也是`Array`的实例,于是`a instanceof Array`也返回`true`。而`mapped``Array.prototype.map`运算的结果,已经是真正的数组,它是`Array`的实例,而不是`MyArray`的实例,于是`mapped instanceof Array`返回`true`,而`mapped instanceof MyArray`返回`false`
### Symbol.match