JavaScript Object-oriented programming three-prototype mode (top) _js Object-oriented

Source: Internet
Author: User
Each function we create has a prototype (prototype) attribute, which is an object that contains properties and methods that can be shared by all instances of a particular type. The benefit of using it is that all object instances can share the properties and methods it contains. That is, instead of defining the object's information in the constructor, you can add the information directly to the prototype object, as shown below, or rewrite the example in the first two logs:
Copy Code code as follows:

function Employee () {
};
Employee.prototype.Name = "Jim";
Employee.prototype.Age = 28;
Employee.prototype.Job = "SoftWare Engineer";
Employee.prototype.SayName = function () {
Alert (this. Name);
};
var employee1 = new Employee ();
Employee1. Sayname ();//jim
var emplayee2 = new Employee ();
Emplayee2. Sayname (); Jim
Alert (employee1. Sayname = Emplayee2. Sayname);//true

Unlike the constructor pattern, these properties and methods of the new object are shared by all instances.
The above is a prototype model of a primer, to understand the working principle of the prototype model, you need to understand the nature of the central ECMAScript.
Understanding the Prototype
In JavaScript, whenever a new function is created, a prototype property is created for the function based on a specific set of rules. By default, although the prototype property automatically obtains a constructor property that contains a pointer to the function of the prototype property, we can continue to add additional properties and methods to the stereotype.
After the custom constructor is created, its prototype property will only get the constructor property by default, and for other methods, it is inherited from object. When the constructor is called to create a new instance, the interior of the instance contains a pointer (internal property) that points to the stereotype property of the constructor. Note that this connection exists between the instance and the constructor prototype property, rather than between the instance and the constructor.
In some implementations, internal properties (_proto_ properties) cannot be accessed, but in all implementations the isPrototypeOf method can be used to determine whether this stereotype relationship exists between objects. In essence, this method returns True if the object's _proto_ property points to isprototypeof. As shown below:
Copy Code code as follows:

Alert (Employee.prototype.isPrototypeOf (EMPLOYEE1)); True
Alert (Employee.prototype.isPrototypeOf (employee2));//true

Whenever the code reads a property of an object, the search is performed, and the target is a property with the given name. The search starts first from the object instance itself. If a property with the given name is found in the instance, then the value of the property, if not found, continues to search for the prototype object that the pointer points to, and finds the property with the given name in the prototype object. If this property is found in the prototype object, the value of the property is returned. This is also the rationale for sharing the properties and methods of a prototype with an object instance.
As mentioned earlier, the prototype initially contains only the constructor attribute, which is also shared, so you can access it through the object instance
Although the values saved in the prototype can be accessed through an object instance, the values in the prototype cannot be overridden through an object instance, if we add a property to the instance that is the same as a property name in the instance prototype. The attribute that name creates in the instance masks the attribute in the (. NET becomes hidden) prototype, as follows:
Copy Code code as follows:

function Employee () {
};
Employee.prototype.Name = "Jim";
Employee.prototype.Age = 28;
Employee.prototype.Job = "SoftWare Engineer";
Employee.prototype.SayName = function () {
Alert (this. Name);
};
Emplayee2. Name = "Sun";
Alert (employee1. Name); Jim
Alert (employee2. Name);//sun

Which employee1. The name of Jim from the prototype, two employee2. Sun from name comes from an instance.
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.