Constructors in JavaScript are different from constructors in other languages. All functions called using the new keyword are considered constructor. Any JavaScript function can be used as a constructor. The constructor must use the new operator as the prefix to create a new instance.
Preface-- Speak before
Like me, I think many beginners who used to have little experience with background programming languages are confused about a series of "Nouns" in javascript. It seems that I know what I'm talking about, but I still don't know what I actually understand. I want to learn any kind of knowledge, first, we should clarify the most basic terms (knowing what it is about helps us better understand it .) It is helpful for advanced learning. The following is a brief introduction of my understanding of such seemingly unimportant but unclear knowledge points. (If there is any discrepancy, please correct me)
1. What is a constructor?
Constructor is a special method. It is mainly used to initialize an object when an object is created, that is, assigning an initial value to the object member variable. It is always used together with the new operator in the statement for creating an object.
This is an explanation provided by Baidu Baike. It is a book-based explanation, but its meaning is still clear. The following is an example:
The Code is as follows:
Var request = new XMLHttpRequest ();
This expression is often used when a request object is created using AJAX technology. We can clearly see that the phrase "new XMLHttpRequest ();" is a standard constructor! "Var" declares a "request" object, and uses the constructor "new XMLHttpRequest ();" to initialize this "request" object and assign it an initial value. Therefore, we can know that "the 'function' used with the 'new' operator to create an object and initialize the object is a constructor ".
For example, the common declared array is the standard constructor: var Array = new array ();
2. What is an instantiated object?
The Code is as follows:
Var request = new XMLHttpRequest ();
In object-oriented programming, the process of creating objects using classes is usually called Instantiation.
I marked the emphasis 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? Literally, we can be understood as "type ". For example, "cake" is a dessert classification, that is, a type. Then, the cheese cake is the specific individual of the cake classification in the dessert, that is, the object.
We know that in programming languages, "Classes" are abstract. We cannot operate on them or use its methods and attributes. Only by instantiating this class into an object, we can call a series of methods and properties. In fact, this is quite understandable. We cannot see or capture abstract things in our lives. Naturally, we cannot use some of its functions, only when abstract things are specific to individual, individual, or actual objects can we clearly understand or understand them. The same is true for programming. Therefore, the instantiation object is a process from abstraction to concrete, which is called Instantiation.
The above is all the content of this article. I hope you will like it.