mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2025-05-24 18:32:22 +00:00
docs(class-extends): fix super
This commit is contained in:
parent
c26d2a55e6
commit
a3a76e79e2
@ -231,7 +231,7 @@ let b = new B();
|
|||||||
|
|
||||||
上面代码中,属性`x`是定义在`A.prototype`上面的,所以`super.x`可以取到它的值。
|
上面代码中,属性`x`是定义在`A.prototype`上面的,所以`super.x`可以取到它的值。
|
||||||
|
|
||||||
ES6 规定,通过`super`调用父类的方法时,方法内部的`this`指向子类。
|
ES6 规定,通过`super`调用父类的方法时,方法内部的`this`指向当前的子类实例。
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
class A {
|
class A {
|
||||||
@ -257,9 +257,9 @@ let b = new B();
|
|||||||
b.m() // 2
|
b.m() // 2
|
||||||
```
|
```
|
||||||
|
|
||||||
上面代码中,`super.print()`虽然调用的是`A.prototype.print()`,但是`A.prototype.print()`内部的`this`指向子类`B`,导致输出的是`2`,而不是`1`。也就是说,实际上执行的是`super.print.call(this)`。
|
上面代码中,`super.print()`虽然调用的是`A.prototype.print()`,但是`A.prototype.print()`内部的`this`指向子类`B`的实例,导致输出的是`2`,而不是`1`。也就是说,实际上执行的是`super.print.call(this)`。
|
||||||
|
|
||||||
由于`this`指向子类,所以如果通过`super`对某个属性赋值,这时`super`就是`this`,赋值的属性会变成子类实例的属性。
|
由于`this`指向子类实例,所以如果通过`super`对某个属性赋值,这时`super`就是`this`,赋值的属性会变成子类实例的属性。
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
class A {
|
class A {
|
||||||
|
@ -449,10 +449,9 @@ CommonJS 模块加载 ES6 模块,不能使用`require`命令,而要使用`im
|
|||||||
// es.mjs
|
// es.mjs
|
||||||
let foo = { bar: 'my-default' };
|
let foo = { bar: 'my-default' };
|
||||||
export default foo;
|
export default foo;
|
||||||
foo = null;
|
|
||||||
|
|
||||||
// cjs.js
|
// cjs.js
|
||||||
const es_namespace = await import('./es');
|
const es_namespace = await import('./es.mjs');
|
||||||
// es_namespace = {
|
// es_namespace = {
|
||||||
// get default() {
|
// get default() {
|
||||||
// ...
|
// ...
|
||||||
@ -462,7 +461,7 @@ console.log(es_namespace.default);
|
|||||||
// { bar:'my-default' }
|
// { bar:'my-default' }
|
||||||
```
|
```
|
||||||
|
|
||||||
上面代码中,`default`接口变成了`es_namespace.default`属性。另外,由于存在缓存机制,`es.mjs`对`foo`的重新赋值没有在模块外部反映出来。
|
上面代码中,`default`接口变成了`es_namespace.default`属性。
|
||||||
|
|
||||||
下面是另一个例子。
|
下面是另一个例子。
|
||||||
|
|
||||||
@ -730,7 +729,7 @@ module.exports = function (n) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
上面代码中,`even.js`加载`odd.js`,而`odd.js`又去加载`even.js`,形成“循环加载”。这时,执行引擎就会输出`even.js`已经执行的部分(不存在任何结果),所以在`odd.js`之中,变量`even`等于`null`,等到后面调用`even(n-1)`就会报错。
|
上面代码中,`even.js`加载`odd.js`,而`odd.js`又去加载`even.js`,形成“循环加载”。这时,执行引擎就会输出`even.js`已经执行的部分(不存在任何结果),所以在`odd.js`之中,变量`even`等于`null`,等到后面调用`even(n - 1)`就会报错。
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ node
|
$ node
|
||||||
|
@ -650,7 +650,7 @@ import {db, users} from './index';
|
|||||||
|
|
||||||
### 简介
|
### 简介
|
||||||
|
|
||||||
前面介绍过,`import`命令会被 JavaScript 引擎静态分析,先于模块内的其他模块执行(叫做”连接“更合适)。所以,下面的代码会报错。
|
前面介绍过,`import`命令会被 JavaScript 引擎静态分析,先于模块内的其他语句执行(`import`命令叫做”连接“ binding 其实更合适)。所以,下面的代码会报错。
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
// 报错
|
// 报错
|
||||||
@ -668,7 +668,7 @@ const path = './' + fileName;
|
|||||||
const myModual = require(path);
|
const myModual = require(path);
|
||||||
```
|
```
|
||||||
|
|
||||||
上面的语句就是动态加载,`require`到底加载哪一个模块,只有运行时才知道。`import`语句做不到这一点。
|
上面的语句就是动态加载,`require`到底加载哪一个模块,只有运行时才知道。`import`命令做不到这一点。
|
||||||
|
|
||||||
因此,有一个[提案](https://github.com/tc39/proposal-dynamic-import),建议引入`import()`函数,完成动态加载。
|
因此,有一个[提案](https://github.com/tc39/proposal-dynamic-import),建议引入`import()`函数,完成动态加载。
|
||||||
|
|
||||||
@ -692,9 +692,34 @@ import(`./section-modules/${someVariable}.js`)
|
|||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
`import()`函数可以用在任何地方,不仅仅是模块,非模块的脚本也可以使用。它是运行时执行,也就是说,什么时候运行到这一句,也会加载指定的模块。另外,`import()`函数与所加载的模块没有静态连接关系,这点也是与`import`语句不相同。
|
`import()`函数可以用在任何地方,不仅仅是模块,非模块的脚本也可以使用。它是运行时执行,也就是说,什么时候运行到这一句,就会加载指定的模块。另外,`import()`函数与所加载的模块没有静态连接关系,这点也是与`import`语句不相同。
|
||||||
|
|
||||||
`import()`类似于 Node 的`require`方法,区别主要是前者是异步加载,后者是同步加载。
|
`import()`类似于 Node 的`require`方法,区别主要是前者是异步加载,后者是同步加载。`import()`的浏览器实现,类似于下面的写法。
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
function importModule(url) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const script = document.createElement("script");
|
||||||
|
const tempGlobal = "__tempModuleLoadingVariable" + Math.random().toString(32).substring(2);
|
||||||
|
script.type = "module";
|
||||||
|
script.textContent = `import * as m from "${url}"; window.${tempGlobal} = m;`;
|
||||||
|
|
||||||
|
script.onload = () => {
|
||||||
|
resolve(window[tempGlobal]);
|
||||||
|
delete window[tempGlobal];
|
||||||
|
script.remove();
|
||||||
|
};
|
||||||
|
|
||||||
|
script.onerror = () => {
|
||||||
|
reject(new Error("Failed to load module script with URL " + url));
|
||||||
|
delete window[tempGlobal];
|
||||||
|
script.remove();
|
||||||
|
};
|
||||||
|
|
||||||
|
document.documentElement.appendChild(script);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### 适用场合
|
### 适用场合
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user