A deeper understanding of the mechanism of JavaScript new

Source: Internet
Author: User

When we use objects, except for some browser built-in monomer objects can be used directly outside, will be new one out of use.

1. The simplest thing to do is to get an instance of object objects as follows

var New Object ();

Description: The most useful thing to do with the new keyword at this point is to inherit all the methods on the Object.prototype, which can be used to see a list of Object.prototype methods in ES5 's resources. This means that the Obj object at this point can use all inherited methods!

2. However, the constructor mode lets us have a deep understanding of new!

function Person (name,age) { This. Name =name;  This. Age =Age ;  This. Sayname =function () {Console.log ( This. Name); }}varPerson1 =NewPerson ("Wang", at);varPerson2 =NewPerson ("Jiang", -);varPerson3 =Newperson;//when the parameters are not passed

It's easy to know that constructors allow each instance to get a copy of the properties and methods on its own. But how did that come about?

What happened in the new process?

1). Create an object.

2). Assigns the scope of the constructor to the new object (so this points to the new object)

3). Execute the code in the constructor (add a property to the new object)

4). Returns the new object

It is important to note that their inheritance is: Person1/person2/person3 inherits from person, and person inherits from object (all objects inherit from object)

If you understand what's above, it's almost enough to meet new in some simple programming.

Let's continue to introduce some new topics

3. Module mode (moudle mode), the following is a simple example of module mode

varCalculator =function (eq) {//You can declare a private member here    varName =2; varEqctl =document.getElementById (eq); return {        //exposing members of the publicName:name, Add:function (x, y) {varval = x +y; Eqctl.innerhtml=Val; }    };};varC1 =NewCalculator ('eq');varC2 =NewCalculator ('La'); C1.name=3the Name property of the//C1 is changed without affecting the name property of C2 Console.log (C1); Console.log (C2); C1.add (2,2);

It is worth noting that an object was returned in the calculator constructor!

Description: If an object is returned in the constructor (4th step) then the new object will be overwritten by the returned object. The result of module mode is that both C1 and C2 copy a copy of the properties and methods from the returned object, and they do not affect each other.

The inheritance also changed, and both C1 and C2 directly inherit from the object

A deeper understanding of the mechanism of JavaScript new

Related Article

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.