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