Home >Web Front-end >Vue.js >Does es6 class have variable promotion?

Does es6 class have variable promotion?

WBOY
WBOYOriginal
2022-04-08 18:43:592241browse

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.

Does es6 class have variable promotion?

The operating environment of this article: Windows 10 system, Vue version 2.9.6, DELL G3 computer.

Does es6 class have variable promotion?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn