This is the 9th day of my participation in the August More Text Challenge

When we use functions, most of the time we use constructors, such as new Array(),

The constructor scenario appears

Looking at the following code, in a scenario where we want to create multiple identical users, we might use the following code:

function createUser(name, age, sex){ return { name, age, sex, sayHello(){ console.log(`my name is ${this.name}, ${this.sex} and I am ${this.age} ')}}} var u1 = twinkle ('twinkle', '18', 'male '); u1.sayHello(); Var u2 = createUser('twinkle', '18', 'twinkle'); u2.sayHello(); console.log(u1 === u2);Copy the code

In the above code, we can create multiple objects, using functions to create objects. (But the two objects are not the same, creating a new object each time) can be much easier, in addition to the above method, there is a constructor way to create objects.

The constructor

Grammar:

New function name (parameter 1, parameter 2...)Copy the code

Modification code:

function User(name, age, sex){ this.name = name; this.age = age; this.sex = sex; this.sayHello = function(){ console.log(`my name is ${this.name}, ${this.sex} and I am ${this.age} ')}} var u1 = new User('twinkle', '18', 'male '); u1.sayHello(); Var u2 = new User('twinkle', '18', 'male '); u2.sayHello(); console.log(u1 === u2);Copy the code

Matters needing attention

  • Constructors are essentially functions that are called differently.

  • The constructor automatically defines a “this” object and returns the current “this” object. If you manually return a primitive type, this will not be returned. If you return an object, this will be replaced.

  • In the built-in objects in JS, all objects are created using the constructor new. Those abbreviations are how literals given by JS are created, which is syntactic sugar;

new.target

Used to determine whether the object is called as a constructor. If not, this expression returns undefined, otherwise it returns the normal constructor.

function U(){ if(new.target ! == U){console.error(' please use new to create functions ')}} U(); new U();Copy the code

The difference between constructors and functions

  • In fact, this point can not be said to be the difference, but in the industry formed a certain standard, ordinary function is the name of the small hump, and the constructor is the name of the big hump;
  • In the CSDN blog, which reviews JS – Function Expressions and This_twinkle, we know that the this pointer of a normal function points to a global object, while the this inside a constructor points to an instance of the current object.
  • The constructor must be called using new. New.target is used to determine whether the constructor is called.

Essence of function

Functions are essentially objects.

function sum  (a,b){
	return a + b;
}

var sum1 = new Function('a'.'b'.'return a + b');

sum(1.3);
sum1(1.3)
Copy the code

All functions are created by new Function.

A wrapper class

To enhance primitive type data, create a constructor for string, Boolean, and number. String, Boolean, Number. In JS, you can think of a class as a constructor.

If a primitive type is syntactically used as an object (typically when using attributes), JS automatically creates an object in that location using the corresponding constructor to access the attributes of the primitive type.

// a.tofixed (2) equals the following code (new Number(a)).tofixed (2);Copy the code

The two variables in the figure above are not strictly equal, two different objects, the object is the reference address, the two variables must be different.

Member property (method), instance property (method), indicating that the property is called from an instance of the constructor (which needs to be called by new).

Static property (method), class property (method), indicating that the property is called by the constructor itself (not by new)