Constructors in JavaScript are different from constructors in other languages. Functions that are called through the New keyword are considered constructors. Any JavaScript function can be used as a constructor, and the constructor must use the new operator as a prefix to create a fresh instance.
Preface--Speak in front
I think there are a lot of beginner friends who used to have little access to the background programming language, like me, confused a series of "nouns" in JavaScript. Seems to know what to say, but the understanding is still not clear, I think, learning any kind of knowledge, the first should be the most basic noun explanation to understand (know what it is speaking, to help us better understand it.) It is known that they know the reason why, so for the future advanced learning is very helpful. The following is a brief look at my own understanding of such seemingly unimportant but clear points of knowledge. (if there are discrepancies, please correct me)
1. What is a constructor
constructor, which is a special method. It is primarily used to initialize objects when they are created, that is, to assign an initial value to an object member variable, always with the new operator in the statement that creates the object.
This is the interpretation of Baidu Encyclopedia, interpretation of the book but the meaning of the expression is very clear. See below for a small example:
The code is as follows:
var request = new XMLHttpRequest ();
This expression is often used when we create a request object when using AJAX technology. So we can see clearly that the phrase "new XMLHttpRequest ()" is a standard constructor! we "Var" declares a "request" object, using the constructor "new XMLHttpRequest ();" To initialize the "requ" EST object assigns it an initial value. So we know that the ' function ' that is used to create an object and initialize the object with the ' new ' operator is the constructor.
For example, our 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 a class is often referred to as instantiation.
Above I marked the point of interpretation with red and blue. To be honest, instantiating an object is the process of creating an Object!
So what is "class"? According to literal understanding we can be understood as "type". For example, "cake", it is a dessert classification, that is, a type; then cheese cake is the specific individual of the cake in the dessert, which is the object.
We know that in the programming language, "class" is abstract, we have no way to manipulate it or use its methods and attributes, only if we instantiate this class as an object, we can invoke its series of methods and properties. Actually this is also very good understanding, abstract things in life we have no way to see it or capture it, then naturally we do not have the means to use some of its functions, only the abstract of things specific to each individual, or the actual object, we can clearly understand or recognize it, programming is the case. Thus, an instantiated object is an abstraction to a specific process, and this process is called instantiation.
The above mentioned is the entire content of this article, I hope you can enjoy.