JavaScript---Prototypes

Source: Internet
Author: User
Tags hasownproperty

Summarize:

JS all functions have a prototype attribute that refers to an object, the prototype object, or a prototype. This function includes constructors and normal functions, and we speak more of the prototype of the constructor, but we cannot deny that the common function is also a prototype.

Constructor we said in the last lesson, in fact, after new, the interior will also be invisible to the prototype properties and prototype methods are assigned to the instance object, and: Remember, they are shared , each of the same new instance object is shared its prototype properties and methods.

Prototype: Prototype

1. Basic

1     functionBOX () {}//constructor Function2Box.prototype.name= ' Lee ';//Prototype Properties3box.prototype.age=100;4box.prototype.run=function(){//Prototyping Methods5         return  This. name+ This. age+ ' Run ';6     }7     8     varbox1=NewBOX ();9     varBox2=NewBOX ();Ten     //alert (Box1.run ());

Answer: Here to mention the function inside, if it is an instance method, different instantiation of their method address is different, is unique, if it is a prototype method, then their address is shared, everyone is the same.

    Alert (box1.constructor)            // construct property, you can get the constructor itself                                    // function is positioned by the prototype pointer, and then get the constructor itself                                    // is actually the object instance corresponding to the role of the prototype object

2. The execution process of prototype mode

1     functionBOX () {}//constructor Function2Box.prototype.name= ' Lee ';//Prototype Properties3box.prototype.age=100;4box.prototype.run=function(){//Prototyping Methods5         return  This. name+ This. age+ ' Run ';6     }7     varbox1=NewBOX ();8Box1.name= ' Jack ';//instance properties, and does not override the Stereotype property9Alert (Box1.name)//Nearest Principle

Answer: There is no doubt that if there is an instance attribute then the instance property/method is accessed first, and the prototype attribute/method is not accessed.

3. Determine if an object instance is pointing to a prototype object, basically as long as it is instantiated, it automatically points to the

isPrototypeOf ()

1     alert (BOX.prototype.isPrototypeOf (box1)); 2     var obj=New  Object (); 3     Alert (Object.prototype.isPrototypeOf (obj));            // true 4     Alert (BOX.prototype.isPrototypeOf (obj));            // false

4. Determine if there is a specified attribute in the instance

hasOwnProperty ()

1     functionBOX () {}//constructor Function2Box.prototype.name= ' Lee ';//Prototype Properties3box.prototype.age=100;4box.prototype.run=function(){//Prototyping Methods5         return  This. name+ This. age+ ' Run ';6     }7     varbox1=NewBOX ();8Alert (Box1.hasownproperty (' name '));//false9Box1.name= ' Jack ';TenAlert (Box1.hasownproperty (' name '));//true

5.in operator

    functionBOX () {}//constructor FunctionBox.prototype.name= ' Lee ';//Prototype Propertiesbox.prototype.age=100; BOX.prototype.run=function(){//Prototyping Methods        return  This. name+ This. age+ ' Run '; }    varbox1=NewBOX (); Alert (' Name 'inchBOX1);//as long as the instance property or prototype property has true

Answer: Returns true as long as the instance attribute or prototype attribute has

Combine 4 and 5: You can write a method that judges only attributes in the prototype

1     functionBOX () {}//constructor Function2Box.prototype.name= ' Lee ';//Prototype Properties3box.prototype.age=100;4box.prototype.run=function(){//Prototyping Methods5         return  This. name+ This. age+ ' Run ';6     }7     functionisproprety (obj,proprety) {8         return!obj.hasownproperty (proprety) && (propretyinchobj);9     }Ten     varbox1=NewBOX (); OneAlert (isproprety (box1, ' name '));//true ABox1.name= "; -Alert (isproprety (box1, ' name '));//false

6. Creating prototype object properties and methods using literals

The prototype object here {}, which represents the new object (), so its prototype object constructs the property Box.constructor point to object without pointing to the constructor box

So:

1     functionBOX () {}2Box.prototype={3         //this is equivalent to creating a new object, and the new object has the constructor property, so the new object's constructor overrides the original box costructor,4         //but you can force it back to box .5 Constructor:box,6Name: ' LEE ',7age:100,8Runfunction(){9             return  This. name+ This. age+ ' Run ';Ten         } One     } A     varbox=NewBOX (); -     //alert (Box.run ()) -Alert (Box.constructor)//function BOX () {}

Answer: Add Constructor:box, and the prototype will be forced back to BOX.

Note: If you override the prototype object, the previous prototype object will be overwritten.

JavaScript---Prototypes

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.