JS's new operator

Source: Internet
Author: User
1. Create an empty object, and the this variable references the object, and also inherits the prototype of the function. 2, properties and methods are added to the object referenced by this. 3. The newly created object is referenced by this and the last implicitly returns this.

var person = function (name) {

var this = {};

THIS.name = name;

This.say = function () {

Return "I am" + this.name;

};

return this;

}

The simple understanding is:

var obj = new Base ();

The equivalent of running the following code, what does the new operator do? In fact, it's very simple, it's done three things.

var obj = {};

obj.__proto__ = Base.prototype;

Base.call (obj);

In the first line, we created an empty object, obj.
In the second line, we point the __proto__ member of this empty object to the base function object prototype member object
In the third line, we replace the this pointer of the base function object with obj and then call the base function, so we assign an ID member variable to the Obj object, which is the value of "base"

JS's new operator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.