[concept] JavaScript constructors and normal functions
What is the difference between a JavaScript constructor and a normal function:
- Differences in invocation modes:
- The constructor needs to be called with the new operator, if the constructor has no arguments, you can omit the parentheses, such as new Object.
- A call to a normal function does not require the new operator, and it must have parentheses. The role of new can be found in a brief introduction to the role of JS's new operator.
- The point of this issue:
- The This of the constructor is bound to the created object instance.
- This of the normal function belongs to the caller of this function.
- Naming method:
- Constructor names usually have a larger initial letter.
- The normal function name is lowercase, using the Hump naming method. constructor function
To create an object using the constructor
In JavaScript, the constructor is actually a normal function. When you use the new operator to function, it can be called a construction method (constructor).
My understanding: constructors are classes. The prototype of the constructor extends the instantiated method. Constructors can be called new
constructors only when called by the operator. Other times, it is a normal function. The particularity of this constructor is not so much the function of the new operator-creating the object and returning the object itself.
The object created by the constructor is no different from the normal var object = {}.
In JavaScript, the general function and the constructor function are compared with the ordinary function, the constructor has the following obvious characteristics
- Called with the New keyword
var prince=new Prince("charming",25);
- Keywords can be used inside a function
this
Inside the constructor, the this
new object that is constructed is pointed to. this
a defined variable or function/method is an instance variable or an instance function/method. You need to use an instance to access it, and you cannot access it with a type name. PRINCE.AGE;//25 prince.age;//undefined
- The default return value constructor does not need to return the value explicitly, and is returned by default, which is the
this
new instance object. Of course, you can also use the return statement, the return value will vary according to the type of return value, details will be described below.
- The name of the function is suggested in uppercase and separated from the normal function area. Not in the naming convention, but it is recommended to write this.
What happens when I instantiate with the new keyword?
The Prince () function in the above article is a chestnut:
- The first step is to create an empty object.
var prince={}
- In the second step, the this in the constructor Prince () points to the newly created object prince.
- The third step is to point the _proto_ property of Prince to the prototype of the Prince function and create the relationship between the object and the prototype.
- Fourth step, execute the code within the constructor prince ().
What if the constructor has a return value?
When return is not explicitly called in the constructor, the default is to return the This object (which must be new
called using the keyword), which is the newly created instance object. When you call return in a constructor, there are two things:
return
are five simple data types: string,number,boolean,null,undefined. In this case, ignoring the return
value still returns the this
object.
return
is an object in this case, the this object is no longer returned, but instead returns the return value of the return statement.
function person (name) { this.name=name; return {name: "Cherry"} } var person=new person ("Sheila"); Person.name;//cherry person;//object {name: "Cherry"}
[concept] JavaScript constructors and normal functions