Constructor and prototype in Javascript, and javascript Constructor
Constructor
Constructor is a special method. It is mainly used to initialize an object when creating an object, that is, assigning an initial value to the object member variable. It is always used together with the new operator in the statement for creating an object. A special class can have multiple constructors, which can be distinguished based on the number of parameters or different parameter types, that is, the overload of constructors.
Example:
<Span style = "font-size: 18px;"> function Box (name, age) {this. name = name; this. age = age; this. run = function () {return this. name + this. age + 'running... ';};} Var box1 = new Box ('lil', 100); var box2 = new Box ('jack', 200); alert (box1.run ()); alert (box1 instanceof Box); </span>
Let's take a look at the declared functions of the constructor.
Prototype
Each function we create has a prototype attribute, which is an object and its purpose is to include attributes and methods that can be shared by all instances of a specific type.
The benefits of using a prototype allow all object instances to share the attributes and methods it contains.
Example:
<Span style = "font-size: 18px;"> function Box () {} Box. prototype. name = 'lil'; Box. prototype. age = 100; Box. prototype. run = function () {return this. name + this. age + 'running... '}; // Compare the method addresses in the prototype with the following: var box1 = new Box (); var box2 = new Box (); alert (box1.run = box2.run ); // The returned result is true, indicating that their reference addresses are the same. </Span>
Let's take a look at the prototype declaration method.
As shown in the figure, the constructor creates an instance in the memory, increasing the memory usage. The prototype mode shares the attributes and methods to reduce the memory burden.
Of course, objects created in the prototype mode also have their own shortcomings. All attributes in the prototype are shared by many instances. Sharing is very suitable for functions and can also contain basic worthwhile attributes. However, some data that needs to be instantiated each time must retain its own features and cannot be shared. This creates a conflict.
Constructor + prototype
To solve this problem, you can combine the constructor + prototype mode.
<Span style = "font-size: 18px;"> function Box (name, age) // Use constructors that are not shared {this. name = name; this. age = age; this. family = ['father ', 'Mother', 'sister '];}; Box. prototype = {// shared use prototype constructor: Box, run: function () {return this. name + this. age + this. family ;}}; </span>By using constructors for non-shared attributes, and for shared methods, the prototype is used to solve the problem of passing parameters and referencing and sharing.
Summary: Object-oriented in JavaScript is similar to object-oriented in. net, but the forms are different. The comparison is similar. Of course, we are still studying for the first time and will continue to learn more in the future to deepen our understanding.