The following example shows how to use instanceof, isPrototypeOf, hasOwnProperty, and in javascript:
<Script type = "text/javascript"> <! -- Function Person () {this. name = "ygm";} var p = new Person (); alert ("Object p belongs to the Object type:" + (p instanceof Object )); alert ("Object p belongs to Person type:" + (p instanceof Person); alert ("does the prototype of object p exist in the prototype chain?" + Person. prototype. isPrototypeOf (p); alert ("Whether the prototype of Object p exists in the prototype chain:" + Object. prototype. isPrototypeOf (p); alert ("whether the object instance has the name attribute:" + p. hasOwnProperty ("name"); alert ("whether the object instance has the toString attribute:" + p. hasOwnProperty ("toString"); alert ("whether the name attribute exists in the object instance or prototype attribute:" + ("name" in p )); alert ("Whether the toString attribute exists in the object instance or prototype attribute:" + ("toString" in p )); // --> </script> <br/> <center> If the result cannot be displayed, press Ctrl + F5 to refresh the page. For more page code: <a href = 'HTTP: // www.bkjia.com/'target = '_ blank'> http://www.bkjia.com/</a> </center>
Tip: the code can be modified before running!