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/02 Report--
This article mainly explains "what are the reasons why JavaScript functions are organized into classes". The explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the reasons for sorting JavaScript functions into classes"?
Getting started with classes in JavaScript
A class in JavaScript is a function, which you would normally write:
Function catName () {console.log ("Tibbers");}
You can also use arrowheads:
CatName = () = > console.log ("Tibbers")
Of course there's no problem with this, but what if you have a bunch of cat-related functions and want to attach them to an instance? What if you don't need to find out and tell the code the specific instance target when you want to do it?
This is where "class" comes in handy.
A class usually has two parts: an attributes and a methods. Property defines the specific instance value of the class. Method to perform specific actions on these properties. The property is set in the constructor, and the method usually appears in the class as a function.
Therefore, the class might look like this:
Class Cat {constructor (name, age, sound) {this.name = name; this.age = age; this.sound = sound;} speak = () = > console.log (this.sound); name = () = > console.log ('hello, my nameis' + this.name); age = () = > console.log ('I am'+ this.age);}
This basically includes 80% of the classes in JavaScript. The other 20% involves different ways to extend and construct classes, which is beyond the scope of this article. The question we are trying to answer is why functions should be organized into classes, rather than just as stand-alone code.
Organized functions are more practical
The purpose of the function is to create scope and boundaries for the code. When you write a function, you're actually saying to the interpreter-- Hey, I want to package something. The process of writing classes is essentially creating another level of organization for your code.
Efficient and practical code depends on the user's ability to organize and convey ideas in a programming language. Because of the loose organization of the JavaScript programming language, the code is easy to fall into a chaotic state. Users can write the same content in many ways, and the code still works.
So the problem of sorting functions into classes is actually an organizational problem. Function is a method of organization, and classes make it develop further.
Prevent mutation
There is a misconception that when you want to protect a variable from a mutation, you need to prevent it from changing.
However, the variation is related to the shape of the variable, not the actual variable itself. The value of a variable can be changed, but the shape cannot.
For example, view the following objects:
Tibbers = {name: "Tibbers", type: "cat", color: "ginger"}
Objects in their current form are not protected from mutation, because you can do this:
Tibbers.owner = "Aphinya"
You can change the shape of the object by adding another key pair value. However, you can prevent this situation if you use classes to instantiate the object Tibbers. You can try to pass a new value, but it won't work.
Why is it so important to prevent mutation? When the shape of the data cannot be changed, it creates a high degree of certainty on the function of the code-making it more functional in the method and reducing the potential side effects of using the object elsewhere.
Everything is based on object
JavaScript is based on the idea of prototype. This means that your code starts with a single global scope, which can be broken down into smaller ring scopes or smaller scopes. The chart looks like this:
Property inherits from parent to child
Therefore, when an object downstream of the chain wants something, it asks for it from its parent. If the parent does not, the process continues to run up the chain until an undefined process is found or returned.
A stand-alone function does not have this ability because it does not have a chain of traversing properties. The function is essentially on the window object and can only access the global scope.
Why not set everything to global scope?
Since you don't need to traverse all the values, once the global scope is declared with JavaScript, it won't disappear in most cases. If the data attached to this variable is quite large, it will slow down the application's speed and ability to handle things efficiently due to insufficient memory space.
Make the function containerized and modularized
Anything can be modular. Practical modularization is related to logical reasoning and the compactness of specific ideas. Anyone can say that the code is modular. You can put your code in a function and point out its modularity.
However, when using classes, you will systematically use mandatory structures and cohesion ideas to deal with modularity. In essence, you are containerizing the code and organizing its functionality to implement a series of related features.
Because this is the method of "classes"-- based on the collection of functions they execute.
JavaScript object oriented
JavaScript is a multi-paradigm language with dynamic types. Multi-paradigm means that it is not limited to a single ideology to construct logic.
This means that JavaScript is not purely functional or object-oriented. It is a language that allows you to mix and match the methods of writing and organizing code to flexibly adapt to logical needs and conditions.
JavaScript's object-oriented design features follow a prototype-based approach, which means that classes can be used for inheritance. JavaScript uses functional methods and object constructs, which means that integrating functional features and functions into classes is a logical arrangement and presentation of business rules in an interrelated manner.
Thank you for your reading, the above is "what are the reasons why JavaScript functions are sorted into classes?" after the study of this article, I believe you have a deeper understanding of the reasons for sorting JavaScript functions into classes, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.