Home >Web Front-end >JS Tutorial >What does it mean to instantiate an object in javascript
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.
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();
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();
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!