[JavaScript] Prototype mode prototype

Source: Internet
Author: User
Tags prototype definition

Each function created in JavaScript has a prototype (prototype) attribute, which is a pointer to an object that is used to contain properties and methods that can be shared by all instances of a particular type. If the literal meaning is understood, then prototype is the prototype object of the object instance created by invoking the constructor. The advantage of using a prototype object is that you can have all object instances share the properties and methods that it contains. In other words, instead of defining the information for an object instance in the constructor, you can add that information directly to the prototype object, as shown in the following example.

1 functionPerson () {2 }3 4Person.prototype.name = "Baka";5Person.prototype.age = 23;6Person.prototype.job = "Student";7Person.prototype.sayName =function() {8Alert This. Name);9 };Ten  One varPerson1 =NewPerson (); APerson1.sayname ();//"Baka" -  - varPerson2 =NewPerson (); thePerson2.sayname ();//"Baka" -  -Alert (Person1.sayname = = Person2.sayname);//ture
View Code

Each time the code reads a property of an object, the search is performed once, and the target is a property with the given name. The search begins first from the object instance itself. If a property with the given name is found in the instance, the value of the property is returned, and if it is not found, the prototype object that the pointer points to is searched, and the property with the given name is looked up in the prototype object. If this property is found in the prototype object, the value of the property is returned. In other words, when we call Person1.sayname (), we perform two searches successively. First, the parser asks: "Does the instance Person1 have a sayname attribute?" "Answer:" No. "Then it continues to search and asks," Does the Person1 prototype have sayname properties? "A:" Yes. So it reads the function that is stored in the prototype object. When we call Person2.sayname (), we will reproduce the same search process and get the same results. This is the rationale for the properties and methods saved by multiple object instances sharing prototypes.

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 an attribute to the instance with the same name as an attribute in the instance prototype, we create the property in the instance that will mask that property in the prototype:

functionPerson () {}person.prototype.name= "Baka"; Person.prototype.age= 23; Person.prototype.job= "Student"; Person.prototype.sayName=function() {alert ( This. name);};varPerson1 =NewPerson ();varPerson2 =NewPerson ();p erson1.name= "Akuma"; alert (person1.name);//"Akuma"-from the instancealert (person2.name);//"Baka"-from the prototype
View Code

When you add a property to an object instance, this property masks the property that is saved in the prototype object with the same name. Even if you set this property to NULL, this property is set only in the instance, not the connection to the prototype. However, using the delete operator allows you to completely remove the instance properties, allowing us to revisit the properties in the prototype.

1 /*2 * Person prototype definition3  */4 5 varPerson1 =NewPerson ();6 varPerson2 =NewPerson ();7 8Person1.name = "Akuma";9alert (person1.name);//"Akuma"Tenalert (person2.name);//"Baka" One  A DeletePerson1.name; -alert (person1.name);//"Baka"
View Code

Use the hasOwnProperty () method to detect whether a property exists in an instance or exists in a prototype. This method returns true only if the given property exists in the object instance.

Use the in operator to determine whether a property exists in an object instance or in a prototype instance.

Simpler prototype syntax:

1 functionPerson () {2 }3 4Person.prototype = {5Name: "Baka",6Age:23,7Job: "Student",8Sayname:function() {9Alert This. Name);Ten     } One};
View Code

[JavaScript] prototype mode prototype

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.