class 声明会提升,但不会初始化赋值。Foo 进入暂时性死区,类似于 let、const 声明变量。
const bar
= new Bar();
function Bar() {
this.bar
= 42;
}
const foo
= new Foo();
class Foo {
constructor() {
this.foo
= 42;
}
}
class 声明内部会启用严格模式。
function Bar() {
baz
= 42;
}
const bar
= new Bar();
class Foo {
constructor() {
fol
= 42;
}
}
const foo
= new Foo();
class` 的所有方法(包括静态方法和实例方法)都是不可枚举的。
function Bar() {
this.bar
= 42;
}
Bar
.answer = function() {
return 42;
};
Bar
.prototype
.print = function() {
console
.log(this.bar
);
};
const barKeys
= Object
.keys(Bar
);
const barProtoKeys
= Object
.keys(Bar
.prototype
);
class Foo {
constructor() {
this.foo
= 42;
}
static answer() {
return 42;
}
print() {
console
.log(this.foo
);
}
}
const fooKeys
= Object
.keys(Foo
);
const fooProtoKeys
= Object
.keys(Foo
.prototype
);
class 的所有方法(包括静态方法和实例方法)都没有原型对象 prototype,所以也没有[[construct]],不能使用 new 来调用。
function Bar() {
this.bar
= 42;
}
Bar
.prototype
.print = function() {
console
.log(this.bar
);
};
const bar
= new Bar();
const barPrint
= new bar.print();
class Foo {
constructor() {
this.foo
= 42;
}
print() {
console
.log(this.foo
);
}
}
const foo
= new Foo();
const fooPrint
= new foo.print();
必须使用 new 调用 class。
function Bar() {
this.bar
= 42;
}
const bar
= Bar();
class Foo {
constructor() {
this.foo
= 42;
}
}
const foo
= Foo();
class 内部无法重写类名。
function Bar() {
Bar
= 'Baz';
this.bar
= 42;
}
const bar
= new Bar();
class Foo {
constructor() {
this.foo
= 42;
Foo
= 'Fol';
}
}
const foo
= new Foo();
Foo
= 'Fol';