-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Open
Labels
Description
class
声明会提升,但不会初始化赋值。Foo
进入暂时性死区,类似于let
、const
声明变量。
const bar = new Bar(); // it's ok
function Bar() {
this.bar = 42;
}
const foo = new Foo(); // ReferenceError: Foo is not defined
class Foo {
constructor() {
this.foo = 42;
}
}
class
声明内部会启用严格模式。
// 引用一个未声明的变量
function Bar() {
baz = 42; // it's ok
}
const bar = new Bar();
class Foo {
constructor() {
fol = 42; // ReferenceError: fol is not defined
}
}
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); // ['answer']
const barProtoKeys = Object.keys(Bar.prototype); // ['print']
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(); // it's ok
class Foo {
constructor() {
this.foo = 42;
}
print() {
console.log(this.foo);
}
}
const foo = new Foo();
const fooPrint = new foo.print(); // TypeError: foo.print is not a constructor
- 必须使用
new
调用class
。
function Bar() {
this.bar = 42;
}
const bar = Bar(); // it's ok
class Foo {
constructor() {
this.foo = 42;
}
}
const foo = Foo(); // TypeError: Class constructor Foo cannot be invoked without 'new'
class
内部无法重写类名。
function Bar() {
Bar = 'Baz'; // it's ok
this.bar = 42;
}
const bar = new Bar();
// Bar: 'Baz'
// bar: Bar {bar: 42}
class Foo {
constructor() {
this.foo = 42;
Foo = 'Fol'; // TypeError: Assignment to constant variable
}
}
const foo = new Foo();
Foo = 'Fol'; // it's ok
XueSeason, xueqingxiao, Mintnoii, yygmind, wuzaofeng and 268 moreSimplyY, meahu, dayTimeAffect, cccjk9527, Bert0324 and 4 moreLivre1431, JayLi52, evan0126, c1026287787, FuXXX-SSS and 6 morezhou1591, zhji2610, evan0126, c1026287787, LeungKaMing and 6 moreDongfengjie, terryMai, IAKGnaHz, jkhll, Brannua and 2 moresunpcm, wlasd4622, evan0126, c1026287787, EternalNiu and 6 morewangtunan, geekyzs, rocky-191, JSluowen, NathanHan1 and 20 morezibuyu69, Hollys31, c1026287787, lingyange, miaogulu and 3 more
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
labike commentedon Feb 22, 2019
@alanchanzm 1.
class
声明会提升 . 是不是写错了?原文: Class declarations, unlike function declarations, are not hoisted.
alanchanzm commentedon Feb 22, 2019
@labike
原文有问题,
class
是会提升的,其表现与let
、const
类似,变量名会进入TDZ。看下例:如果没有提升,
foo
会是块作用域外的Foo
实例。但是由于提升的关系,块作用域内的Foo
遮蔽了外层的同名函数。[-]第 7 期:ES5/ES6 的继承除了写法以外还有什么区别?解答与一个疑惑[/-][+]第 7 期:ES5/ES6 的继承除了写法以外还有什么区别?[/+]labike commentedon Feb 23, 2019
@alanchanzm 我觉得不对吧
class会提升这段代码就说不过去!
class
alanchanzm commentedon Feb 23, 2019
@labike
可能是我们对「提升」的理解不同吧?我理解的「提升」和「赋值」是两个过程。
我拆解一下那个例子:
类似于以下代码(但不等于):
XueSeason commentedon Feb 23, 2019
@alanchanzm 答了很多,而且很有帮助,但是离题了。
问题是继承的差异。
子类可以直接通过 __proto__ 寻址到父类。
而通过 ES5 的方式,Sub.__proto__ === Function.prototype
alanchanzm commentedon Feb 23, 2019
@XueSeason 哈哈哈,审题不清,这轮面试要挂了。
再补充一点:
ES5 和 ES6 子类
this
生成顺序不同。ES5 的继承先生成了子类实例,再调用父类的构造函数修饰子类实例,ES6 的继承先生成父类实例,再调用子类的构造函数修饰父类实例。这个差别使得 ES6 可以继承内置对象。xiaofengqqcom123 commentedon Feb 27, 2019
因为this生成顺序不同,所以需要在constructor中,需要使用super()
xiaofengqqcom123 commentedon Feb 27, 2019
因为this生成顺序不同,所以需要在constructor中,需要使用super()
xiaofengqqcom123 commentedon Feb 27, 2019
因为this生成顺序不同,所以需要在constructor中,需要使用super()
xiaofengqqcom123 commentedon Feb 27, 2019
因为this生成顺序不同,所以需要在constructor中,需要使用super()
xiaofengqqcom123 commentedon Feb 27, 2019
因为this生成顺序不同,所以需要在constructor中,需要使用super()
xiaofengqqcom123 commentedon Feb 27, 2019
因为this生成顺序不同,所以需要在constructor中,需要使用super()
xiaofengqqcom123 commentedon Feb 27, 2019
因为this生成顺序不同,所以需要在constructor中,需要使用super()
xiaofengqqcom123 commentedon Feb 27, 2019
因为this生成顺序不同,所以需要在constructor中,需要使用super()
80 remaining items