Home > Article > Web Front-end > What are constructors and instantiated objects in javascript?
Preface
I think there are many beginner friends who have rarely been exposed to backend programming languages like me, and they are confused by a series of "nouns" in JavaScript. Fog. It seems that I roughly know what it is about, but in fact I still don’t understand it clearly; I think that when learning any kind of knowledge, the first step should be to clarify the most basic terminology (knowing what it is about will help us better understand it) (Understand it.) That is, knowing what it is and why it is, will be very helpful for future advanced learning. Below I will briefly talk about my own understanding of some seemingly unimportant but necessary knowledge points. (If there are any discrepancies, please correct me)
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 our Often used when creating 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: var array = new Array();
2. What is an instantiated object
var request = new XMLHttpRequest();
In oriented In object programming, the process of creating an object using a class is usually 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 languages, "Class" is abstract, and we have no way to operate it Or use its methods and properties. 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 with the object can we clearly understand or recognize it; The same is true for programming. Therefore, instantiating an object is a process from abstract to concrete. This process is called instantiation.
The above is the detailed content of What are constructors and instantiated objects in javascript?. For more information, please follow other related articles on the PHP Chinese website!