Skip to content

Commit bd863cd

Browse files
committed
fix(class): edit class/super
1 parent ded5622 commit bd863cd

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

docs/class.md

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -724,26 +724,62 @@ Object.getPrototypeOf(ColorPoint) === Point
724724

725725
因此,可以使用这个方法判断,一个类是否继承了另一个类。
726726

727-
### super关键字
727+
### super 关键字
728728

729729
`super`这个关键字,有两种用法,含义不同。
730730

731731
(1)作为函数调用时(即`super(...args)`),`super`代表父类的构造函数。
732732

733-
(2)作为对象调用时(即`super.prop``super.method()`),`super`代表父类。注意,此时`super`既可以引用父类实例的属性和方法,也可以引用父类的静态方法
733+
(2)作为对象调用时(即`super.prop``super.method()`),`super`代表父类。注意,此时`super`只能引用父类实例的方法(包括静态方法),不能引用父类的属性
734734

735735
```javascript
736+
class A {
737+
p() {
738+
return 2;
739+
}
740+
}
741+
736742
class B extends A {
743+
constructor() {
744+
super();
745+
this.p = 3;
746+
}
747+
737748
get m() {
738-
return this._p * super._p;
749+
return this.p * super.p();
739750
}
740-
set m() {
741-
throw new Error('该属性只读');
751+
752+
set m(value) {
753+
throw new Error('read only');
742754
}
743755
}
756+
757+
let b = new B();
758+
b.m // 6
759+
```
760+
761+
上面代码中,子类通过`super`关键字,调用父类实例的`p`方法。
762+
763+
如果`p`是父类实例的属性,那么`super`无法引用到它。
764+
765+
```javascript
766+
class A {
767+
constructor() {
768+
this.p = 2;
769+
}
770+
}
771+
772+
class B extends A {
773+
get m() {
774+
return super.p;
775+
}
776+
}
777+
778+
let b = new B();
779+
b.m // undefined
744780
```
745781

746-
上面代码中,子类通过`super`关键字,调用父类实例的`_p`属性
782+
上面代码中,`p`是父类`A`实例的属性,`super.p`就引用不到它
747783

748784
由于,对象总是继承其他对象的,所以可以在任意一个对象中,使用`super`关键字。
749785

docs/destructuring.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ baz // "aaa"
223223
foo // error: foo is not defined
224224
```
225225

226-
上面代码中,真正被赋值的是变量`baz`,而不是模式`foo`
226+
上面代码中,`foo`是匹配的模式,`baz`才是变量。真正被赋值的是变量`baz`,而不是模式`foo`
227227

228228
注意,采用这种写法时,变量的声明和赋值是一体的。对于`let``const`来说,变量不能重新声明,所以一旦赋值的变量以前声明过,就会报错。
229229

0 commit comments

Comments
 (0)