Home > Web Front-end > JS Tutorial > body text

What does it mean to instantiate an object in javascript

青灯夜游
Release: 2021-10-09 13:49:38
Original
10707 people have browsed it

In js, the process of creating an object using a class is usually called instantiation, and instantiating an object is the process of creating an object. "Class" is abstract. There is no way to operate it or use its methods and attributes. Only by instantiating this class into an object can you call a series of its methods and attributes; therefore, instantiating an object is from abstract to specific process.

What does it mean to instantiate an object in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

1. What is a constructor?

Constructor is a special method. Mainly used to initialize the object when creating the object, that is, assign the initial value to the object member variable. It is always used together with the new operator when creating the object. in the statement.

This is the explanation from Baidu Encyclopedia. The explanation is very bookish but the meaning is still very clear. Please see a small example below:

var request = new XMLHttpRequest();
Copy after login
Copy after login

This expression is often used when we create request objects when using AJAX technology. Then we can clearly see that "new XMLHttpRequest();" is a standard constructor! We "var" declared a "request" object , using the constructor "new XMLHttpRequest();" Initialize the "request" object and assign it an initial value. So we can know: " Used with the 'new' operator The 'function' used to create and initialize the object is the constructor".

For example, our common declaration array is the standard constructor: <span style="font-size: 16px">var array = new Array(); </span>

2. What is an instantiated object

var request = new XMLHttpRequest();
Copy after login
Copy after login

In object-oriented programming, usually The process of creating an object with a class is called instantiation.

I highlighted the key points of the explanation in red and blue. To put it bluntly, instantiating an object is the process of creating an object!

So what is "Class"? According to the literal understanding, we can understand it as "Type". For example, "cake" is a dessert category, is also a type; then cheesecake is the specific individual of the category cake in dessert, which is the object.

We know that in programming language, "Class" is abstract. We have no way to operate it or use its methods and properties, only Only by instantiating this class into an object can we call a series of its methods and properties. In fact, this is also easy to understand. In life, we have no way to see or capture abstract things, so naturally we have no way to use some of its functions. We can only concrete the abstract things into each individual, individual or actual situation. Only when we have an object can we clearly understand or recognize it; The same is true for programming. Therefore, instantiating an object is the process from abstraction to concreteness. This process is called instantiation.

The above is the detailed content of What does it mean to instantiate an object in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template