JavaScript implements methods for migrating shared attributes to prototypes _javascript tips

Source: Internet
Author: User

When we create an object with a constructor, its properties are added to this. And the properties that are added to this are not actually going to change with the entity, at which point it would seem inefficient. For example:

function her () {
  this.name = ' Anna ';
}

This means that every time we create an instance object with new her (), a new name property is generated and has its own storage space in memory. In fact, we can add the name attribute to the prototype so that all instances can share the Name property:

function her () {}
her.prototype.name = ' Anna ';

Thus, when we create an object with new her (), the Name property is no longer a private property of the new object, but is added to the object's prototype. Although this approach can be very efficient, but this is also for the instance object immutable attributes, this is certain otherwise, change this attribute, all the new objects created by this property will be changed, this is not what we want AH ~ ~ ~ The public properties of an object are particularly appropriate for this method.

Next, let's improve an example of the previous one:

function her () {};
Her.prototype.name = ' Anna ';
her.prototype.toString = function () {return
  this.name;
}

function his () {};
His.prototype = new Her ();
His.prototype.constructor = his;
His.prototype.sex = ' women ';

As you can see, we have now completed the construction of the related inheritance work before we do the prototype object extension, otherwise the subsequent new attribute methods in His.prototype may erase the inherited things.

function child (f, m) {
  this.eat = f;
  This.don = m;
}
Child.prototype = new His ();
Child.prototype.constructor = child;
Child.prototype.name = ' Jok ';
Child.prototype.fun = function () {return
  this.eat + This.don
}

As you can see, the difference between actually calling ToString () is only a small amount of action behind the scenes. The main difference is the attribute, and the search for methods will occur more frequently in her.prototype.

The above JavaScript will share attributes migrated to the prototype to achieve the method is to share all the content of the small, hope to give you a reference, but also hope that we support the cloud habitat community.

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.