JavaScript---Migrating shared properties to prototypes

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 will not actually change with the entity, at this point, this approach is inefficient. For example:

function She () {   this. Name = ' Anna ';} 

This means that every time we create an instance object with new her, we generate a completely new Name property, and we have the storage space in memory that belongs to that property. In fact, we can add the name attribute to the prototype so that all instances can share the Name property:

function= ' Anna ';

In this way, when we create the object with new her (), the Name property is no longer a private property of the new object, but is added to the prototype of the object. While this can be efficient, it's also for immutable properties in the instance object, which is certain, otherwise change this property, 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.

Let's refine the previous example:

function= ' Anna 'function() {    returnthis. Name;} function  new== ' women ';

As you can see, we usually build the inheritance work before we do the prototype object extension, otherwise the subsequent new attribute methods in His.prototype might erase the inherited stuff.

function Child (f, m) {   this. Eat = f;     this. Don =new== ' Jok 'function() {     returnthis. Don}

As you can see, the difference between actually calling ToString () is only a small number of operations behind the scenes. The main difference is the attribute, and the method's lookup will occur more in Her.prototype.

Good sleepy, a day is very depressed, do not knock, early to sleep, and then back. ( ̄┰ ̄*)

JavaScript---Migrating shared properties to prototypes

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.