What is new?

The new operator creates an instance of a user-defined object type or an instance of a built-in object with a constructor

What does New do?

  1. Create an empty simple JavaScript object (that is, {});
  2. Link this object (that is, set its constructor) to another object;
  3. Use the newly created object in Step 1 as the context for this;
  4. If the function does not return an object, it returns this.

Implementation of new

Read my code

1. Let obj = {} or let obj = new object(); 2. Obj. Proto = func. prototype (Func = constructor) 3. Change this to access the function’s internal variable with apply

function New() {
    letobj = {}; // Create object console.log(arguments);letconstructor = [].shift.call(arguments); // Get the constructor console.log(arguments);if(constructor.prototype ! == null) { obj.__proto__ = constructor.prototype; // The constructor links to the new object}letret = constructor.apply(obj, [].slice.call(arguments)); // Change this to point to console.log(arguments); console.log(typeof ret);if ((typeof ret === "object" || typeof ret === "function") && ret ! == null) {return ret;
    }
    returnobj; // If the function does not return Object type Object(including Functoin, Array, Date, RegExg, Error), then the function call in the new expression returns a reference to that Object. }function name(a, b) {
	this.a = a;
	this.b = b;
}

let c = New(name, 1, 2)
let d = new name(1, 2)
console.log(c);
console.log(d);
Copy the code

Let’s look at the return value:

We see that New and New have the same effect

Note: [].shift.slice() changes our arguments. typeof null == “object”

Important: Why do we need to make object judgments? This is usually ignored when implementing new.

By default, functions return undefined (if no return value is explicitly defined), but constructors are an exception. The new constructor returns the newly created object by default without a return. However, if the return value is of the basic data type (string, number, Boolean, undefined, null), the return value is still the newly created object, which is a bit strange. The return value of the function is the specified object only if the display returns an object that is not of a basic data type. In this case, the object referenced by the this value is discarded.

Here are two examples:

Case 1:

Example 2:

// return; / / return this

// return null; / / return this

// return this;

// return []; / / return []

// return function(){}; // Return this function, discarding this

// return false; / / return this

// return new Boolean( false); // Return a new Boolean; Abandon this

// return ‘hello world’; / / return this

// return new String( ‘hello world’); // Return the new string, discarding this

// return 2; / / return this

// return new Number( 32); // Return the new number, discarding this

What is arguments?

Arguments is an array-like object corresponding to the arguments passed to the function.

The Arguments object can only be used within functions

[] what does.slice.call() do?

Convert arguments to an array

Similar conversion methods

var args = Array.prototype.slice.call(arguments);

var args = [].slice.call(arguments);

// ES2015

const args = Array.from(arguments);

const args = […arguments];

MDN does not recommend that we do slice solutions for parameters:

function New() {
    letobj = {}; // Create object console.log(arguments);letconstructor = [].shift.call(arguments); // Get the constructor console.log(arguments);if(constructor.prototype ! == null) { obj.__proto__ = constructor.prototype; }let ret = constructor.apply(obj, (arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments)));
    console.log(ret);
    console.log(arguments);
    if ((typeof ret === "object" || typeof ret === "function") && ret ! == null) {return ret;
    }
    return obj;
}

function name(a, b) {
	this.a = a;
	this.b = b;
}

let c = New(name, 1, 2)
let d = new name(1, 2)
console.log(c);
console.log(d);
Copy the code

Using Slice for parameters can prevent optimizations in some JavaScript engines. If you are concerned about performance, try constructing a new array by traversing the Arguments object. Another approach is to use the neglected Array constructor as a function

[].shift. Call ()

Get the first argument of arguments and change the length of arguments

What did Slice do

Return a new array containing the elements of arrayObject from start to end (excluding that element).

. The slice () method

Definition and usage The slice(start, end) method extracts a part of an array and returns the extracted part as a new array.

Use start (included) and end (not included)

Parameter to specify the beginning and end of the extraction array.

If start and end are not specified, the entire array is returned.

If you specify an argument that is used as start, return the entire array including the start position.

If it is negative, this parameter specifies the position from the end of the array. In other words, -1 is the last term in the exponent group, -2 is the penultimate term, and so on.

What call does (the implementation puts call, apply, and bind together)

Change the reference to this in []

What does apply do (the implementation will put call, apply, bind together)

Change the direction of this

How does this. Point to change

We point this to the newly created object by referring apply to apply

A method to convert an object with a length attribute to an array

Array. The form () or… Or traverse

What should I pay attention to when converting array.from

Array-like objects have key values that are numbers

How is Slice implemented?

Array.prototype.slice = function(start,end){ var result = new Array(); start = start || 0; end = end || this.length; // This points to the calling object. When we use call, we can change this to point to the incoming object. This is the keyfor(var i = start; i < end; i++){
          result.push(this[i]);
     }
     return result;
Copy the code