Home > Web Front-end > JS Tutorial > body text

The difference between JavaScript isPrototypeOf and hasOwnProperty_js object-oriented

WBOY
Release: 2016-05-16 18:33:11
Original
852 people have browsed it
1. isPrototypeOf
isPrototypeOf is used to determine whether the specified object object1 exists in the prototype chain of another object object2. If so, it returns true, otherwise it returns false.
The format is as follows:
object1.isPrototypeOf(object2);
object1 is an instance of an object;
object2 is another object whose prototype chain will be checked.
Prototype chains can be used to share functionality between different instances of the same object type.
If object2’s prototype chain contains object1, then the isPrototypeOf method returns true.
If object2 is not an object or object1 does not appear in the prototype chain in object2, the isPrototypeOf method will return false.
Usage examples are as follows:
Copy code The code is as follows:

var re = /^ s*/;
// Define a regular expression object here
// Check whether RegExp is the prototype chain object of re and return true
var bIsptt = RegExp.prototype.isPrototypeOf(re);

2. hasOwnProperty
hasOwnProperty determines whether an object has a named attribute or object. This method cannot check whether the object has this attribute in the prototype chain. This attribute must Is a member of the object itself.
If the property or method is defined by the object itself rather than in the prototype chain, return true; otherwise, return false;
The format is as follows:
object.hasOwnProperty(proName);
Judgement The name of proName is not a property or object of the object object. Examples of usage are as follows:
Copy code The code is as follows:

// Get false because it cannot be detected Properties in the prototype chain
var bStr = "Test String".hasOwnProperty("split");
// The prototype of the String object already has this property, so it naturally returns true
var bStr1 = String. prototype.hasOwnProperty("split");
// Returns true because the property in the prototype is not detected
var bObj = ({fnTest:function(){}}).hasOwnProperty("fnTest");
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template