Why Object.prototype.isPrototypeOf ?
The isPrototypeOf () method checks whether an object exists in another object hierarchy. This returns boolean values. If true returns, that object is in control of the specified object. If False returns, that object is not in control of the specified object.
object.isPrototypeOf( obj )
- obj:The object prototype to be checked against
Object. - object: The object chain to be checked in for the prototype.
returns:
trueifobjectis the prototype ofo;falseifois not an object or ifobjectis not the prototype ofo.
JavaScript Object.prototype.isPrototypeOf Examples
Example 1
function myObject1() { }
function myObject2() { }
myObject1.prototype = Object.create(myObject2.prototype);
let myObject3 = new myObject1();
console.log(myObject1.prototype.isPrototypeOf(myObject3));
console.log(myObject2.prototype.isPrototypeOf(myObject3));
output:
true
true
Example 2
function per1() { };
function per2() { };
function per3() { };
per1.prototype = Object.create(per2.prototype);
per2.prototype = Object.create(per1.prototype);
var MyObject = new per3();
console.log(per3.prototype.isPrototypeOf(per3));
console.log(per1.prototype.isPrototypeOf(per3));
console.log(per2.prototype.isPrototypeOf(per3));
console.log(Object.prototype.isPrototypeOf(per1));
output:
false
false
false
true
Sources:
Browser Support
| Chrome | yes |
| Edge | yes |
| Firefox | 1 |
| Internet Explorer | yes |
| Opera | yes |
| Safari | yes |
| Android webview | yes |
| Chrome for Android | yes |
| Edge mobile | yes |
| Firefox for Android | 4 |
| Opera Android | yes |
Leave a comment