Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between the constructors of es6 and es5

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "what are the differences between the constructors of es6 and es5". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope this article "what are the differences between the constructors of es6 and es5" can help you solve the problem.

The difference: 1, the variable of the class in the es6 constructor will not be promoted, that is, the object can only be created after the definition of the class, while the declaration of the constructor in es5 will improve the variable; 2, es6 cannot call the constructor directly, the constructor can be called directly in es5, and the constructor can be used as an ordinary function.

This tutorial operating environment: windows10 system, ECMAScript version 6. 0, Dell G3 computer.

What is the difference between the constructors of es6 and es5

Using constructors to construct reusable objects

The constructor is the function you construct, which is a special method, which is qualitatively different from the ordinary function. When creating an object, it is mainly used to initialize the object, that is, to assign initial values to the object members. The main characteristics of the constructor are the method name, capitalization of the initials, and the use of new.

ES5function foo () {this.name = 'Katherine'; this.age =' 26 years;} var f = new foo (); console.log (f) / / Objectconsole.log (f.name) / / Katherineconsole.log (f.age) / / 26function foos (name,age,sex) {this.name = name; this.age = age; this.sex = sex } var f1 = new foos ('Kathrine',' 26, 'female'); var f2 = new foos (' Stefan','27, 'male'); var f3 = new foos (' Damon','29, 'male') Console.log (F1) / / foos {name: "Kathrine", age: "26", sex: "female"} console.log (f2) / / foos {name: "Stefan", age: "27", sex: "male"} console.log (f3) / / foos {name: "Damon", age: "29", sex: "male"} ES6class foo {constructor () {this.name = 'Karherine' This.age ='26 this.age+' years old';} vampire (va) {console.log ('Her name is' + this.name+' and she was'+ this.age+' years old')} let f = new foo () f.vampire (); / / Her name is Karherine and she was 26 years old// inheritance prototype class foos extends foo {constructor () {super () This.name = 'Stefan'; this.age =' 27 years old;}} let F1 = new foos (); f1.vampire (); / / His name is Stefan and he was 27 years old

1. ES5 can use new to generate objects, or you can call the constructor directly and use it as an ordinary function. Such as the function foo ()

2. ES6 must use new to generate objects, and cannot directly call the constructor to be used as an ordinary function.

Unlike ES5, the variables of a class are not promoted, which means that objects can only be created after the class has been defined.

Class calls must use new, while ordinary constructors can be used as ordinary functions.

This is the end of the introduction to "what are the differences between the constructors of es6 and es5". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report