In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
The excellent practice of creating classes in JavaScript and when to create classes, I believe that many inexperienced people do not know what to do. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Using default parameters and property abbreviations, it's easy to clean up our JavaScript code.
Constructor function
In order for our constructor to work better, we should do something. They are:
Initialize all member data in all constructors, if possible
We should put them all in the constructor so that they are all initialized when the object is instantiated.
So we can write like this:
Class Person {constructor (name) {if (this.instance) {this.instance = {name}} return this.instance;}}
Now we make sure that everything is initialized with a value.
Create a singleton in the constructor
If we only need one instance of a constructor, we can create an instance.
For example, we can write the following code:
Class Person {constructor (name) {if (this.instance) {this.instance = {name}} return this.instance;}}
In the above code, if this.instance is not already defined, we return the object we created.
Otherwise, we will return anything that is set to this.instance.
Give priority to deep copy over shallow copy
A deep copy copies everything, so it is much better than a shallow copy, which leaves something that references the original object.
If we want a real copy, that's not good.
Therefore, we have to write code to make a deep copy, as follows:
Const copy = obj = > {const copied = {... obj}; for (const k of Object.keys (obj)) {if (typeof obj [k] = 'object') {copied [k] = {... copied [k]}; copy (copied [k]);}} return copied;}
If we find a nested object, we only use the extension operator to copy the nested object and do the same operation recursively.
Then we return to the object we copied.
When should we create a class?
We should not always create classes. In some scenarios, it makes sense to create a class.
Objects that simulate the real world
Classes are ideal for modeling real-world objects because they can model the behavior of objects.
They allow us to encapsulate instance variables and methods in a package that stores state and performs operations on objects, respectively.
Modeling abstract objects
Similarly, we can use classes to model abstract objects.
They can be used for abstraction, which is a generalization of different types of objects.
Classes are ideal for storing shared members of subclasses, and subclasses can inherit them.
However, we should keep the inheritance tree simple so that people will not be confused by the code.
Reduce complexity
We can use classes to reduce the complexity of the program.
Class is ideal for hiding information. In JavaScript, there are no private variables in the class, so we have to hide the data in the method.
In this way, we can minimize the coupling between different parts of the program.
Hide implementation details
Method is also suitable for hiding implementation details.
We can hide the details in the method and run only what we need.
To do this, we can nest functions and variables within the method.
Limit the side effects of change
Because we can hide things, we can reduce the impact of changes.
As with hiding the implementation, you can isolate the impact of changes by limiting the impact of changes within a method.
Hide global data
By putting global data into the methods of the class, they can become private data.
In this way, they do not have to be open to the public. All we have to do is declare them in the method using let and const.
Simplify parameter transfer
If we pass the same parameter to a different function, we can change the parameter to the instance variable and the function to the method.
For example, if we have:
Const speak = (name) = > `$ {name} spoke`; const greet = (name) = > `hi, ${name} `
We can then put the method into our own class, as follows:
Class Person {constructor (name) {this.name = name;} speak () {return `${this.name} spoke`;} greet () {return `spoke`, ${this.name}`;}}
Now, we don't have to pass name anywhere.
We just need to create an instance of Person and call these methods without passing in any parameters.
We can create classes to encapsulate the data and package the content together. However, we should not create classes for everything.
In addition, we should copy as deeply as possible instead of shallowly.
After reading the above, have you mastered the good practice of creating a class in JavaScript and how to create a class? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.