[JS Master's Road] prototype object (prototype) and prototype chain related properties and methods

Source: Internet
Author: User
Tags hasownproperty

One, instanceof:

instanceof detects if there is a prototype prototype on the right side of the __PROTO__ prototype chain on the left. I've been in two previous articles

[JS Master's Road] the basic characteristics and advantages and disadvantages of the constructor function

[JS Master's Road] step-by-step graphical JavaScript prototype (prototype) object, prototype chain

have been shared.

1         functionCreateobj (uName) {2              This. UserName =UName;3              This. Showusername =function () {4                 return' 100 ';5             }6         }7CreateObj.prototype.showUserName =function () {8             return  This. UserName;9         }Ten         varObj1 =NewCreateobj (' Ghostwu '); One         varObj2 =NewCreateobj (' Wei Zhuang ')); A  -Console.log (obj1instanceofCreateobj);//true -Console.log (obj2instanceofCreateobj);//true theConsole.log (obj1instanceofObject);//true -Console.log (obj2instanceofObject);//true

Second, isprototypeof:

If the implicit prototype __proto__ points to an object prototype (Createobj) that calls the isPrototypeOf () method, then this method returns true, such as:

1         var New Createobj (' Ghostwu '); 2         var New Createobj (' Wei Zhuang '); 3         // true 4         //

Because obj1,obj2 of the implicit prototype __proto__ point is Createobj.prototype, a friend may ask Createobj.prototype above there is no isprototypeof this method, how can

Call it?

Yes, yes, but Createobj.prototype's implicit prototype __proto__ points to Object.prototype, and isprototypeof exists on object.prototype, so it can call

Third, object.getprototypeof

Gets the pointer to the implicit prototype (__proto__) of the instance, because Obj1,obj2 's __proto__ all point to Createobj.prototype

1         var New Createobj (' Ghostwu '); 2         var New Createobj (' Wei Zhuang '); 3         // true 4         // true

Four, when the instance accesses properties and methods, follows the nearest lookup principle

Instance first find in their own body, there, stop looking, if not, along the instance of the __proto__ continue to look up, there, stop looking, if not continue along the prototype chain to look up, if

None of the prototype objects, that is undefined.

1         functionCreateobj (uName) {2              This. UserName =UName;3         }4CreateObj.prototype.showUserName =function () {5             return  This. UserName;6         }7CreateObj.prototype.age = 22;8 9         varObj1 =NewCreateobj (' Ghostwu ');TenObj1.age = 20; One         varObj2 =NewCreateobj (' Wei Zhuang ')); A  -Console.log (Obj1.age);//---> From example -Console.log (Obj2.age);//---> From prototype objects the  -         DeleteObj1.age; -Console.log (Obj1.age);//---> From prototypes

Five, hasOwnProperty

Determines whether the property is on an instance or on a prototype object, and returns true on the prototype if it is on the instance, false on the original

1         functionCreateobj (uName) {2              This. UserName =UName;3         }4CreateObj.prototype.showUserName =function () {5             return  This. UserName;6         }7CreateObj.prototype.age = 22;8         varObj1 =NewCreateobj (' Ghostwu ');9Obj1.age = 20;Ten         varObj2 =NewCreateobj (' Wei Zhuang ')); OneConsole.log (Obj1.age);//---> From example AConsole.log (Obj1.hasownproperty (' age '));//true -Console.log (Obj2.age);//---> From prototype objects -Console.log (Obj2.hasownproperty (' age '));//false the         DeleteObj1.age; -Console.log (Obj1.age);//---> From prototypes -Console.log (Obj1.hasownproperty (' age '));//false

VI, in operator

Determines whether the property is on an instance or prototype object, and the return value is true whenever a condition is met

1         functionCreateobj (uName) {2              This. UserName =UName;3         }4CreateObj.prototype.showUserName =function () {5             return  This. UserName;6         }7CreateObj.prototype.age = 22;8         varObj1 =NewCreateobj (' Ghostwu ');9Obj1.age = 20;TenConsole.log (' Age 'inchOBJ1);//true One         varObj2 =NewCreateobj (' Wei Zhuang ')); AConsole.log (' Age 'inchOBJ2);//true -         DeleteObj1.age; -Console.log (' Age 'inchOBJ1);//true theConsole.log (' user 'inchOBJ1);//false -Console.log (' user 'inchOBJ2);//false

Seven, in combination with the use of in and hasownproperty, can encapsulate a function to determine whether this property is on the prototype object, the return value is true: on the Prototype object, false: Not on the prototype object

1         functionCreateobj (uName) {2              This. UserName =UName;3         }4CreateObj.prototype.showUserName =function () {5             return  This. UserName;6         }7CreateObj.prototype.age = 20;8         functionhasprototypeproperty (obj, name) {9             return!obj.hasownproperty (name) && (nameinchobj);Ten         } One         varObj1 =NewCreateobj (' Ghostwu '); A         varObj2 =NewCreateobj (' Wei Zhuang ')); -Obj1.age = 10; -Console.log (Hasprototypeproperty (obj1, ' age '));//false theConsole.log (Hasprototypeproperty (obj2, ' age '));//true

For...in can enumerate properties and methods on instances and prototype objects, provided that the property and method are enumerable

1          functionCreateobj (uName) {2              This. UserName =UName;3         }4CreateObj.prototype.showUserName =function () {5             return  This. UserName;6         }7CreateObj.prototype.age = 20;8         varobj =NewCreateobj (' Ghostwu ' );9 Ten          for(varKeyinchobj) { OneConsole.log (key);//Username,age,showusername A         } - Console.log (object.prototype); -          for(varKeyinchObject.prototype) { theConsole.log (key);//Cannot enumerate, the properties and methods on Object.prototype are not enumerated by default, the enumeration property is False -}

[JS Master's Path] prototype object (prototype) and prototype chain related properties and methods detailed

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.