Simple understanding of prototype objects in JavaScript, implementing shared properties and behaviors between pairs

Source: Internet
Author: User

Constructors are provided in JavaScript. The object can be easily created.

Typical constructors are as follows:

function person (name, age) {   this.name = name;  This.age = age; This.say = function () {return this.name  + ', ' + this.age;;}

You can then create multiple objects with the new and constructor functions. in JavaScript. Properties and methods are independent between different objects of a class. What do you mean? Member variables are independent between different objects in a class in Java (each object has its own storage space). Store property values). But the method is shared, and there is only one copy in memory. But in JavaScript, the method is also a part of the object. Assuming that multiple objects are created, the methods in each object will open up new space in memory, thus wasting more space.

var p1= new person (' a ', +); var p2= new person (' B ', 22); Alert (P1.say = = P2.say);//The result returns false, indicating that the method memory space is also different



There are no static and member variables in JavaScript, and it is assumed that data or methods can be shared between objects, only with the help of prototype objects. The shared variables and methods are placed in the prototype object.

function User (name,age) {    this.name = name;    This.age = age;} User.prototype.addr = ' Shenzhen ';//Add attribute User.prototype.show = function () {//In prototype) Add Method    alert (this.name+ ' | ') +this.age);    }; var = new User (' ZXC ', user1), var user2 = new User (' CXZ ', +); alert (user1.show = = user2.show);//Returns True to indicate that the show method is shared

JS's constructor has a prototype attribute that points to its prototype object (in fact, it is a normal JS object).

An object created from the same constructor. Share the same prototype object. The prototype object is empty when initialized. We are able to define ourselves in the inside, regardless of the properties and methods, these methods and properties will be inherited by the object created by that constructor.

Suppose the prototype has changed. All instances will then change.

Simple understanding of prototype objects in JavaScript, implementing shared properties and behaviors between pairs

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.