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--
Today, I will talk to you about what is wrong with the class of JavaScript. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Although the JavaScript class looks fine, if you use it for a while, especially those who have used ES5 before, you will probably see the prototype inheritance evolve into the current class pattern.
Why? What's wrong with the prototype chain? In my humble opinion, the answer is that everything is fine. But the technical community took years to force the concept of classes into different structures and libraries, so the ECMA Technical Committee decided to add it anyway.
Is there a problem with that? Add some components to the prototype inheritance we already have and decide to call it a class, which in turn makes developers think they are dealing with an object-oriented language when they are not.
Classes are just syntax sugars (syntactic sugar)
JavaScript does not have the full support of OOP because it never requires OOP. On the face of it, the current class layout presents the OPP paradigm because:
You can define basic classes, classification status and behavior, and special classical syntax.
You can extend one class to another.
You can define the feasibility of properties and methods, both public and private.
You can define the winner and setter for the property.
You can ignore methods inherited by the class.
Of course, various categories can also be instantiated.
I call a class syntactic sugar because, although on the face of it, the class looks very object-oriented, if you do something that goes beyond the domain possibility, such as defining a class that involves the other two categories (which is currently impossible), you need to use the following code:
/ / Thehelper function functionapplyMixins (derivedCtor,baseCtors) {baseCtors.forEach (baseCtor = > {Object.getOwnPropertyNames (baseCtor.prototype) .forEach (name= > {let descriptor = Object.getOwnPropertyDescriptor (baseCtor.prototype, name) Object.defineProperty (derivedCtor.prototype, name, descriptor);});}) } / / The parent classes classA {methodA () {console.log ("A")}} classB {methodB () {console.log ("B")} / / The child class classC {} / / Using mixins applyMixins (C [a, B]) let o = newC () o.methodA () o.methodB ()
We need to do this because the JS cannot be edited:
ClassA {methodA () {console.log ("A")}} classB {methodB () {console.log ("B")}} classCextendsA, B {}
In some cases, this behavior may come in handy. JavaScript employees created the above code snippet, and I just deleted the extra code to make it applicable to normal JS.
But the important information of the sample code should be the applyMixins function. Even if its function is not fully understood, it can be found that it is used to evaluate various kinds of prototype properties to copy and reassign methods and properties. This is all the evidence for discovering the facts: classes are just syntactic sugar on top of a validated prototype inheritance model.
Does this mean you should stop using classes? It's not. It is important to understand it, and if you need to break the boundaries between what a class can and cannot do, you will have to deal with prototypes to achieve this.
What did JavaScript's OOP model miss?
If the current OOP model is not perfect and is just an abstraction inherited by the prototype, what are we missing? What makes JS a real OOP?
To answer this question, you need to look at the functions of JavaScript, and the team behind the language must invent something that can convert JavaScript into JS to push JavaScript to the limit. This, in turn, limits their functionality, but a good way to start OOP wish lists is to look at their OOP-related features.
You will soon notice a warning: some of the OOP constructs that are currently missing in JavaScript have inherent type checking capabilities and have no real meaning in dynamically typed languages, probably because they haven't been added yet.
Interface
These are good structures that help define the API that the class should follow. Interfaces can be lost in untyped JS, and one of its main benefits is that you can define a variable for any class that implements the same interface and safely call any of its methods.
InterfaceAnimal {speak ()} classDog implements Animal {speak () {console.log ("Woof!")}} classCat implements Animal {speak () {console.log ("Meau!")}} ClassHuman implements Animal {speak () {console.log ("Hey dude What's up? ")}} / / if we had Interfaces in JS we could safely do: let objects = [newDog (), newCat (), newHuman ()] objects.forEach (o = > o.speak ())
This cannot be done in a normal JS. Of course, you can do the same by defining a speak method and overriding its class. But then again, you can do this in any other strong OOP language, making the interface clearer and cleaner.
Abstract class
Whenever I try to do full OOP in code, I'm sure to miss the abstract classes in JS. Abstract classes define and implement methods, but are never instantiated. It is a way to group common behaviors that can be extended but cannot be used directly. It can definitely be implemented in the current JS domain without causing too much damage.
Static polymorphism
Static polymorphism allows us to define the same method multiple times in the same class, but use different signatures. In other words, repeat the name, but make sure it receives different parameters. Now we have the rest parameter using JS, which allows us to have any number, however, it also means that additional code must be added to the method to handle this level of dynamics.
On the contrary, if you can distinguish method signatures more clearly, you can directly encapsulate different styles of the same behavior into different methods.
The above version is invalid JS, but its code is cleaner, so there is less cognitive load for psychoanalysis. However, the following version is completely valid. It requires some mental compounding, and there is more code around it, because it not only logs (which should be its only purpose), but also tries to decide how to log based on the parameters provided.
Static polymorphism is usually achieved by looking at the types of parameters received in the method. However, because of the way JS works, we know this is impossible.
Protected properties and methods
You already have public visibility, and you quickly get private visibility of methods and properties. The next step should be to add protected visibility, all of which are necessary if you want to have an appropriate OOP experience. Protected properties and methods can only be accessed from within the class or from one of its subclasses (in contrast to private visibility, private visibility restricts access to the parent class).
I've been trying to call JS the OOP language, and I won't keep trying until I see a way to handle the inside of a class without referring to a prototype chain. Why can't they continue to extend the prototype inheritance model instead of giving us this cheap version of the class? This is a long-standing problem.
After reading the above, do you have any further understanding of what's wrong with JavaScript's class? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.