Home > Article > Web Front-end > Does es6 class have variable promotion?
ES6 classes do not have variable promotion. There is variable promotion in class in es5. You can use it first and then define the class. In es6, the declaration of the class will not be promoted to the head. If you use it in the definition first, an error will be reported. Therefore, you cannot use it first and then define it. es6 class There is no variable promotion.
The operating environment of this article: Windows 10 system, Vue version 2.9.6, DELL G3 computer.
Class does not have variable promotion
Because ES6 does not promote the class declaration to the head of the code, it needs to be defined first and then used. .
But ES5 is different. ES5 has variable promotion, which can be used first and then defined.
// ES5可以先使用再定义,存在变量提升 new A(); function A(){ } // ES6不能先使用再定义,不存在变量提升 会报错 new B(); // B is not defined class B{ }
There is no variable hoist (hoist) in the class, which is completely different from ES5.
new Foo(); // ReferenceError class Foo {}
In the above code, the ES6 Foo class is used first and defined later. This will cause an error because ES6 will not promote the class declaration to the head of the code. The reason for this provision is related to the inheritance mentioned below. It is necessary to ensure that the subclass is defined after the parent class.
{ let Foo = class {}; class Bar extends Foo { } }
The above code will not report an error, because when Bar inherits Foo, Foo is already defined. However, if there is a class promotion, the above code will report an error, because the class will be promoted to the head of the code, but the let command is not promoted, so when Bar inherits Foo, Foo has not been defined.
[Related recommendations: "vue.js Tutorial"]
The above is the detailed content of Does es6 class have variable promotion?. For more information, please follow other related articles on the PHP Chinese website!