3.Javascript achieve instanceof

instanceof

instanceof for determining whether an object is an instance of another object (constructor method). instanceof will find the prototype chain until null if not behind instance of this object if it returns false, otherwise it returns true

. 1  function instanceofFunc (obj, cons) {
 2    // error determination constructor must be a function of other average error 
. 3    IF ( typeof cons! == 'function') the throw  new new Error ( 'error instance' )
 . 4    IF (! Obj || ( typeof obj! == 'Object' && typeof obj! == 'function')) return  to false 
. 5    // acquired prototype object 
. 6    the let proto = cons.prototype
 . 7    // If the prototype object obj is not null 
. 8    the while (.__ proto__ obj) {
 . 9      IF (obj .__ proto proto__ ===)return true
10     obj = obj.__proto__
11   }
12   return false
13 }
14 
15 console.log(instanceofFunc(() => {}, Function)) // true

 

Guess you like

Origin www.cnblogs.com/xiaole9924/p/11836528.html