这篇文章会零零散散过一些使用类语法中的问题。
1)类不能当做函数调用
class Point{}
Point();//Uncaught TypeError: Class constructors cannot be invoked without 'new'
2)类没有hoisting过程
new Foo(); // ReferenceError
class Foo {}
3)类表达式写法:
const MyClass = class Me {
getClassName() {
return Me.name;
}
};
let inst = new MyClass();
console.log(inst.getClassName()); // Me
console.log(Me.name); // ReferenceError: Me is not defined
4)方法名称可以为变量
class Foo() {
['my'+'Method']() {}
}
const m = 'myMethod';
class Foo() {
[m]() {}
}
5)类中定义的方法不能用new来调用
class C {
m() {}
}
new C.prototype.m(); // Uncaught TypeError: m() {} is not a constructor
6)super可以用在对象字面量中
var parent = {
method(){
console.log("Parent");
}
},child = {
method(){
super.method();
console.log("Child");
}
}
Object.setPrototypeOf(child,parent);
child.method();//Parent Child