In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what is the basic usage of class in the new features of es6". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the basic usage of class in the new features of es6"?
In the javaScript language, the traditional method of generating instance objects is through constructors, which is very different from the traditional object-oriented languages (such as C++ and Java). ES6 provides a writing method closer to the traditional language, and introduces the concept of class (class) as the template of the object. Through the class keyword, you can define classes.
Object-oriented differences between es6class and es5:
1. It is written differently, using the keyword class
two。 When you new an instance, there is a constructor method by default, and the instance object (this) is returned by default, or another object can be returned.
3. All methods of the class are on the prototype attribute, but cannot be enumerated, and semicolons cannot be used at the end of each method
4. The class must be called through an instance of new, and the internal of the class uses strict mode by default.
5. There is no variable promotion, it must be declared before calling
The this of 6.class points to the current class by default
The static method of 7.class, which uses the keyword static, does not need new, and is called directly through the class
8. Instance attributes and static attributes are written in equality (=) directly inside the class, or in the constructor method. Static attributes only need to be preceded by the keyword static.
9. Class inheritance uses the keyword extends, and the inheritance mechanism is completely different from es5.
The inheritance principle of es5: first new the instance object this of the subclass, and then add the methods and properties of the parent class to the this of the subclass (parents.call (this)).
The inheritance principle of Es6: first create the instance object this of the parent class, so to construct the function constructor () to access the properties of the parent class using this, you must first call the super () method, and then modify the this through the constructor () of the subclass
10. Class inheritance can inherit the native constructor, but es5 cannot
General writing of es6class and es5
Copy the code
/ / I. how to write ES5:
FunctionAnimate (name) {
This.name=name
}
Animate.prototype.getname=function () {
Console.log (this.name)
}
Varp=newAnimate ("lity")
P.getname ()
/ / II. ES6, object-oriented writing, calss
ClassPerson {
/ / constructor (): the constructor is the default method, which is automatically called when new is called. If it is not explicitly defined, it will be automatically added.
/ / 1. Suitable for initialization data
/ / 2.constructor can specify the returned object
Constructor (name,age) {
This.name=name
This.age=age
}
At this point, I believe you have a deeper understanding of "what is the basic usage of class in the new features of es6". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.