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 is the function of Object in JavaScript

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces the relevant knowledge of what the role of Object in JavaScript is, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on the role of Object in JavaScript. Let's take a look at it.

Three elements of Object Identifier-- unique identification

State-statu

Behavior-behavior

In fact, philosophers will study an Object, such as what the unique logo of the fish is, and all the bones of the fish are picked out to see whether it is the fish or not. Then cut off all the meat, and then put it together to see if it is this fish, this is the famous philosophical question "the ship of Theseus".

We do not need to care about this, let's say that the variable has a unique identity, and this is also a core element of the object.

The object must have a state, the state can be changed, and the change is the behavior. Thus the three elements of the object are established.

Any concept in our mind and any object in reality can become an object, as long as the three elements are complete.

Object-- Class (class)

First of all, Class classes and Type types are two different concepts.

An important way for us to understand objects is called classification, and we can describe objects in a classified way. For example, after we study and test a fish, its characteristics are similar to all the fish of the same type, so we can group these fish into one category, called "Fish Class".

In fact, there is a larger classification of fish as "animal classification (Animal)", so there are other animal classifications under animals, such as sheep (Sheep). So the commonness between fish and sheep will be described as "animals". Then we go layer by layer of abstraction, and there will be Object on top of "Animal".

Class is a very common way to describe objects, such as the organisms we just talked about. With objects, all organisms can be divided into genera and species, which is a huge classification system. When writing code, classification is a service for the business, we do not need to divide it in so much detail. Usually we will write the common needs in the code, we will put forward the Animal, we will no longer distinguish the mammal, egg laying, or chordate, and so on.

There are two schools of classification, one is classification, the other is classification.

Categorization-- that is, we study a single object, and then we extract commonalities from them into classes, and then we extract commonalities between classes and turn them into higher abstract classes. For example, we extract the commonness in "sheep" and "fish", and then extract the sharing between them into the category of "animals". For the "classification" method, multi-inheritance is very natural, such as diamond inheritance in C++, triangle inheritance and so on.

Classification-abstracts everything in the world into a base class Object, and then defines what is in the Object. The computer language that adopts the idea of classification is a single inheritance structure. And there will be a base class Object.

The language JavaScript is close to the idea of classification, but it is not exactly the idea of classification, because it is a multi-paradigm object-oriented language.

Object-Prototype (prototype)

Next, let's talk about the way JavaScript describes objects.

In fact, the classification of Class Based Object is not the only way to recognize objects, we also have a closer to human natural cognition. The ability to classify may not be available until at least primary school. But after we know the object, we can get another way to describe the object almost immediately. That's the prototype.

The prototype is understood in terms of "painting a tiger according to a cat". In fact, painting a tiger according to a cat is a prototype method used. Because cats and tigers are very similar, we only need to distinguish them directly from each other.

For example, if we want to study fish now, then find a typical fish, such as a specific carp, and then we add all the characteristics of this carp to the prototype of the fish. As long as other fish have an object, we will modify it according to the prototype of the fish. For example, catfish can eat more than carp, it eats meat, and its body is slippery, so we can add these features to the prototype of carp, so we can describe catfish.

So among the sheep, we also choose a little sheep as our basic prototype. Then if we find a goat and we analyze that its characteristics are bearded, curved feet, long, hard and able to climb mountains, then we add these characteristics to the prototype of the little sheep, then we describe a goat.

Well, among the superior "animals", we also choose a typical animal, such as a tiger, which has four hooves, but not all animals have four hooves, but it is relatively free to choose a prototype. For example, if we choose a snake as the prototype of an animal, we have a lot of trouble describing fish and even more about cats.

There will also be a final prototype called Object Prototype, which is the typical object of all objects, or the ancestor of all our objects. We describe any object in terms of the difference between it and the description object.

Then there are generally no more prototypes on top of Object Prototype, but there are some languages that allow a Nihilo prototype. Nihilo means nothingness and emptiness, which is a language neutral way of saying. If we use the specific facilities of JavaScript to describe, then the Nihilo prototype is null, which is easy for everyone to understand, we can easily have a prototype of null objects.

Small summary:

Our prototype is a way to describe objects that are closer to human primitive cognition.

Therefore, all kinds of object-oriented methods are not absolutely right or wrong, but only have different costs in different scenarios.

The cognitive cost of the prototype is low, and the cost of choosing the wrong one is also relatively low, so the prototype is suitable for some scenarios that are not so clear and freely described.

While Class is more suitable for some more rigorous scenarios, Class has an advantage that it is naturally integrated with the type system, so many languages will choose to integrate the inheritance relationship of Class into the inheritance relationship of the type system.

A little exercise

If we need to write a "dog bites" Class, how do we design it?

If we follow a relatively simple method, we will define a Dog Class, and then give the Class a bite method.

Class Dog {bite (Human) {/ /. }}

Such a piece of code is exactly the same as our title, but this abstraction is a wrong abstraction. Because this violates the basic characteristics of object-oriented, no matter how we design it, as long as the bite happens to the dog, it is wrong.

Why?

Because we talked about the three elements of object-oriented, the state of the object must be changed by the behavior of the object itself. So if we write bite in the dog's Class, but the state is changed to "human", after the dog bites people, it will only cause harm to people. So the state of the "person" changes in this behavior, so if the behavior is in the dog's Class, it violates the object-oriented feature.

Of course, if dogs eat people, then we can barely be established, because dogs eat people and dogs are full, and there is a change in the state of dogs. But when a dog bites, we can basically assume that this behavior has no effect on the dog's state.

So we should design a behavior in the Class of "people". Then some students will ask, should we add a biteBy behavior to people? Is it an act of being bitten? It also seems to be wrong, because the behavior in the Class should be used to change the state of the person, so what should the name of this behavior be?

A more reasonable behavior here would be that hurt indicates that he has been hurt, and then pass in the parameter of this behavior is the degree of injury damage. Because people here only care about how much damage he has suffered, he doesn't need to care whether it is bitten by a dog or something.

Class Human {hurt (damage) {/ /. }}

Dog bite is a business logic in the actual development scenario, we only need to design the behavior to change the internal state of the human Human object, so the correct name should be hurt. The damage here can be calculated or generated from the behavior method of biting bite in the dog Class, but if we pass the object of the dog Dog directly, it certainly does not conform to our abstract principle of the object.

In the end, our code implementation logic is as follows:

Class Human {constructor (name = 'human') {this.name = name; this.hp = 100;} hurt (damage) {this.hp-= damage; console.log (`${this.name} suffered ${damage} points of damage, ${this.hp}` for the rest of his life);}} class Dog {constructor (name = 'dog') {this.name = name; this.attackPower = 10 / / attack force} bite () {return this.attackPower;}} let human = new Human ('three diamonds'); let dog = new Dog (); human.hurt (dog.bite ()); / / output: three diamonds take damage at 10:00, 90 for the rest of life

Principles of the design object:

We should not be disturbed by language descriptions (especially business requirements)

When designing the state and behavior of objects, we always follow the principle of "behavior changes the state".

If you violate this principle, the cohesion of the whole object will be lost, which will cause great damage to the architecture.

This is the end of the article on "what is the role of Object in JavaScript". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "what is the role of Object in JavaScript". If you want to learn more knowledge, you are welcome to follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report