What is the new keyword in JavaScript?

Technology CommunityCategory: JavaScriptWhat is the new keyword in JavaScript?
VietMX Staff asked 3 years ago
  1. It creates a new object. The type of this object is simply object.
  2. It sets this new object’s internal, inaccessible, [prototype] (i.e.__proto__) property to be the constructor function’s external, accessible, prototype object (every function object automatically has a prototype property).
  3. It makes the this variable point to the newly created object.
  4. It executes the constructor function, using the newly created object whenever this is mentioned.
  5. It returns the newly created object, unless the constructor function returns a non-null object reference. In this case, that object reference is returned instead.

Consider:

function New(func) {
    var res = {};
    if (func.prototype !== null) {
        res.__proto__ = func.prototype;
    }
    var ret = func.apply(res, Array.prototype.slice.call(arguments, 1));
    if ((typeof ret === "object" || typeof ret === "function") && ret !== null) {
        return ret;
    }
    return res;
}