Constructor and prototype in Javascript, and javascript Constructor

Source: Internet
Author: User

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.


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.