The benefits of using the use prototype (prototype) Definition method in JS

Source: Internet
Author: User
Tags prototype definition

Often interviewing at the front or communicating with other peers is the best way to use a prototype when it comes to constructing a constructor in JS: Defining a method on the prototype of a constructor method, the benefit is that the instance that is generated by the constructor has a method that points to the index of a function. This will save memory.

Of course, there is no problem with this statement, but in implementation, not only the use of prototype way to achieve this effect, we can define the method in the form of a function outside the constructor, and then in the constructor through This.method = method, The methods of this generated instance are also directed to a function by index, as follows:

// do not use the prototype definition method: (function() {    function  constractor () {        this. method1=  method1;         this. method2 = method2;    }     function method1 () {    }    function  method2 () {   }}) ();

The code used to define the prototype generally is as follows:

(function  () {    function  constractor () {    }    = {          function() {        },        method2:  function() {        }    };         // or    function () {    };     function () {    };}) ();

There is nothing advanced in theory and implementation, but in order to achieve the same goal, it can be done in different ways.

The benefits of using the use prototype (prototype) Definition method in JS

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.