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

How to understand the object-oriented in JavaScript

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to understand the object-oriented in JavaScript". The content in the 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 "how to understand the object-oriented in JavaScript".

JavaScript is object-oriented. But many people do not have a comprehensive understanding of this.

In JavaScript, there are two types of objects. One can be called "ordinary objects", which we generally understand: numbers, dates, user-defined objects (such as: {}), and so on.

Another kind, called "method object", is what we usually define as function. You may wonder: the method is the method, how has it become the object? But in JavaScript, methods are indeed treated as objects. Here is a simple example:

Function func () {alert ('hellograms');}

Alert (func.toString ())

In this example, func is defined as a method, but it itself contains a toString method, indicating that func is treated here as an object. More precisely, func is a "method object". The following is a continuation of the example:

Func.name = "I am func."

Alert (func.name)

We can set properties for func at will, which further proves that func is an object. So what's the difference between a method object and a normal object? First of all, the method object is of course executable, and the method object is executed by adding a pair of parentheses after it.

Func ()

Therefore, the method object has a duality. On the one hand, it can be executed, on the other hand, it can be used as a normal object. What does that mean? This means that method objects can exist completely independent of other objects. We can compare this with Java. In Java, methods must be defined in a class and cannot exist alone. Not in JavaScript.

A method object is independent of other methods, which means that it can be arbitrarily referenced and passed. Here is an example:

Function invoke (f) {

F ()

}

Invoke (func)

Pass one method object func to another method object invoke so that the latter executes func when appropriate. This is the so-called "callback". In addition, the particularity of the method object also makes the this keyword difficult to grasp. There are a lot of related articles in this area, so I won't repeat them here.

In addition to being executable, a method object has a special function, which is that it can create ordinary objects through the new keyword.

It is said that when each method object is created, it will automatically have a property called prototype. There is nothing special about this property. Like other properties, it is accessible and can be assigned. But when we use the new keyword to create an object, prototype works: all the properties contained in its value (which is also an object) are copied to the newly created object. Here is an example:

Func.prototype.name= "prototype of func"

Var f = new func ()

Alert (f.name)

Two dialog boxes pop up during execution, the latter indicating that f, the newly created object, copied the name property from func.prototype. The previous dialog box indicates that func was executed as a method. You may ask, why do you still execute func at this time? In fact, the execution of func at this time is to play the role of a "constructor". For the sake of illustration, let's start all over again:

Function func () {

This.name= "name has been changed."

}

Func.prototype.name= "prototype of func"

Var f = new func ()

Alert (f.name)

You will find that the name attribute of f is no longer "prototype of func", but has been replaced with "name has been changed". This is the role of the "constructor" played by the func object method. So, in JavaScript, creating an object with the new keyword performs the following three steps:

Create a new normal object

Copy all the properties of the prototype property of the method object to the new normal object.

Executes the method object in the context of the new normal object.

Statements such as "new func ()" can be described as "create a new object from func". All in all, the only special thing about the prototype property is that it's time to create a new object.

Then we can take advantage of that. For example, there are two method objects An and B. since the new object created from A contains all the properties of A.prototype, I assign it to B.prototype, so doesn't the new object created from B have the same properties? That's what it's like to write code:

A.prototype.hello = function () {alert ('hellograms');}

B.prototype = new A ()

New B () .hello ()

This is the so-called "inheritance" of JavaScript, which is essentially a copy of attributes, which is realized by using prototype. If you don't use prototype, you use loops, and the effect is the same. The so-called "multiple inheritance" is naturally copied everywhere.

These are the principles of object orientation in JavaScript. I never mentioned the concept of "class" from beginning to end, because there is no such thing as "class" in JavaScript. Can object-oriented be without classes? Yes, of course. It is unreasonable to have a class first and then an object, because the class is originally induced from the object, and it is reasonable to have the object first and then the class. Something like this:

Var o = {}; / / I found something.

O.eat = function () {return "I am eating."} / / I find it eats

O.sleep = function () {return "ZZZzzz..."} / / I find it sleeps

O.talk = function () {return "Hi!"} / / I find it can talk

O.think = function () {return "Hmmm..."} / / I find it can still think.

Var Human = new Function (); / / I decided to name it "Man".

Human.prototype = o; / / this thing represents all the concepts of "people".

Var h = new Human (); / / when I find other things like it

Alert (h.talk ()) / / I knew it was also "human"!

Thank you for your reading, the above is the content of "how to understand the object-oriented in JavaScript". After the study of this article, I believe you have a deeper understanding of how to understand the object-oriented in JavaScript, 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.

Share To

Development

Wechat

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

12
Report