A Brief introduction to javascript constructors and Instantiated objects

  • 2020-06-19 09:41:32
  • OfStack

Preface -- in the front

I think there are a lot of beginner friends who have little experience with background programming languages in the past and have no idea about the "nouns" in javascript. I seem to have a general idea of what I'm talking about, but I still don't understand it clearly. I think the first step in learning any kind of knowledge is to explain the most basic nouns (knowing what they mean helps us understand them better). That is, knowing what it is is very helpful for the advanced learning in the future. The following is a brief introduction to my own understanding of these seemingly unimportant but necessary knowledge points. (If there is any discrepancy, please correct me)

1. What is a constructor

The constructor is a special method. It is mainly used to initialize the object when the object is created, that is, to assign an initial value to the object member variable. It is always used with the new operator 1 in the statement of creating the object.

This is the explanation of Baidu Encyclopedia, the explanation is very bookish but the meaning of expression is very clear. Here are a few examples:


  var request = new XMLHttpRequest();

This expression is often used when creating request objects using THE AJAX technique. Then we can clearly see "new XMLHttpRequest();" This sentence is a standard constructor! We "var" declare an "request" object with the constructor "new XMLHttpRequest();" To initialize the "request" object to give it an initial value. So we know that "the 'function' used to create and initialize objects with the 'new' operator 1 is the constructor".

For example, our common declaration array is the standard constructor: var array = new Array();

2. What is an instantiation object


  var request = new XMLHttpRequest();

In object-oriented programming, the process of creating objects with classes is often referred to as instantiation.

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

So what is a class? According to the literal meaning, we can think of it as "type". For example, "cake", which is the classification of 1 dessert, namely 1 type; Then cheesecake is the specific individual, the object, of the classification of cakes in desserts.

We know that in the programming language, "class" is abstract, and we have no way to manipulate it or use its methods and properties. Only by instantiating the class as an object can we call its 1 series of methods and properties. In fact, this is also very easy to understand, in the life of abstract things we can not see it or capture it, then naturally we have no way to use some of its functions, only concrete abstract things to 1, individual or actual objects, we can clearly understand or recognize it; The same is true for programming. Thus, instantiating an object is the process of going from an abstraction to a concrete object. This process is called instantiation.

This is the end of this article, I hope you enjoy it.


Related articles: