JavaScript Object-Oriented Programming three prototype mode (I) _ js object-oriented

Source: Internet
Author: User
The factory mode and constructor mode are introduced in javaScript object-oriented design and Javascript object-oriented design, as well as their advantages and disadvantages, today, we will continue to explain the prototype mode. Each function we create has a prototype attribute, which is an object that contains attributes and Methods shared by all instances of a specific type. The advantage of using it is that it allows all object instances to share the attributes and methods it contains. That is to say, you do not need to define the object information in the constructor, but you can use this information, add it directly to the prototype object, as shown below, or rewrite the example in the previous two logs:

The Code is 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 mode, these attributes and methods of the new object are shared by all instances.
The above is an introduction to the prototype mode. To understand the working principle of the prototype mode, you need to understand the nature of the original type of ECMASCRIPT.
Understanding prototype
In Javascript, once a new function is created, a prototype attribute is created for the function based on a specific set of rules. By default, even though the prototype attribute automatically obtains a constructor attribute, this attribute contains a pointer to the function of the prototype attribute. Through this constructor, we can also add other attributes and methods for the prototype.
After a custom constructor is created, only the constructor attributes are obtained by default. Other methods are inherited from objects. After a constructor is called to create a new instance, the instance contains a pointer (internal attribute) pointing to the constructor's prototype attribute. 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 attributes (_ proto _ attributes) cannot be accessed, but in all implementations, The isPrototypeOf method can be used to determine whether the prototype relationship exists between objects. In essence, if the object's _ proto _ property points to isPrototypeOf, this method returns true. As follows:

The Code is as follows:


Alert (Employee. prototype. isPrototypeOf (employee1); // true
Alert (Employee. prototype. isPrototypeOf (employee2); // true


Every time the Code reads an attribute of an object, it performs a search. The target is an attribute with a given name. The search starts with the object instance itself. If an attribute with a given name is found in the instance, the value of the attribute is then found. If no value is found, search for the prototype object pointed to by the pointer, search for attributes with a given name in the prototype object. If this attribute is found in the prototype object, the value of this attribute is returned. This is also the basic principle of the attributes and Methods saved by sharing the prototype of an object instance.
As mentioned above, the prototype initially only contains the constructor attribute, which is also shared. Therefore, it can be accessed through an object instance.
Although the values stored in the prototype can be accessed through the object instance, the values in the prototype cannot be overwritten through the object instance. If we add an attribute to the instance, this attribute is the same as an attribute name in the instance prototype. The attributes created by name in the instance are blocked (. the property in the prototype is as follows:

The Code is 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


Here, Jim of employee1.Name comes from the prototype, and sun of 2. employee2.Name comes from the 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.