In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to manually implement instanceof in JavaScript. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
1. The usage of instanceof
The instanceof operator is used to detect whether the prototype property of the constructor appears on the prototype chain of an instance object.
Function Person () {} function Person2 () {} const usr = new Person (); console.log (usr instanceof Person); / / trueconsole.log (usr instanceof Object); / / trueconsole.log (usr instanceof Person2); / / false
As in the above code, two constructors, Person and Person2, are defined, and the practical new operation creates an instance object of Person, usr.
Use the instanceof operator to verify that the prototype property of the constructor is on the prototype chain of the usr instance, respectively.
Of course, it turns out that the prototype properties of Person and Object are on the prototype chain of usr. Usr is not an instance of Person2, so the prototype property of Person2 is not on the prototype chain of usr.
two。 Implement instanceof
Once you understand the function and principle of instanceof, you can implement a function with the same function as instanceof:
Implicit prototype of function myInstanceof (obj, constructor) {/ / obj let implicitPrototype = obj?.__proto__; / / prototype of the constructor const displayPrototype = constructor.prototype; / / traverses the prototype chain while (implicitPrototype) {/ / found and returns true if (implicitPrototype = = displayPrototype) return true; implicitPrototype = implicitPrototype.__proto__ } / / the traversal has not been found yet. Return false return false;}
The myInstanceof function takes two parameters: the instance object obj and the constructor constructor.
First get the implicit prototype of the instance object: the prototype object constructor.prototype of the obj.__proto__, constructor.
Then, by constantly getting the next level of implicit prototypes:
ImplicitPrototype = implicitPrototype.__proto__
To traverse the prototype chain, find out if displayPrototype is on the prototype chain, and if found, return true.
When implicitPrototype is null, the search ends, no find is found, and false is returned.
A prototype chain is actually a data structure similar to a linked list.
What instanceof does is look for a target node on the linked list. Start from the header node and continue to traverse backwards, and if you find the target node, return true. At the end of the traversal, it has not been found. Return false.
3. Verification
Write a simple example to verify your own implementation of instanceof:
Function Person () {} function Person2 () {} const usr = new Person (); function myInstanceof (obj, constructor) {let implicitPrototype = obj?.__proto__; const displayPrototype = constructor.prototype; while (implicitPrototype) {if (implicitPrototype = displayPrototype) return true; implicitPrototype = implicitPrototype.__proto__;} return false;} myInstanceof (usr, Person); / truemyInstanceof (usr, Object); / / truemyInstanceof (usr, Person2) / / falsemyInstanceof (usr, Function); / / falsemyInstanceof (usr.__proto__, Person); / / falseusr.__proto__ instanceof Person; / / false
As you can see, myInstanceof got the results correctly.
This is the end of the article on "how to manually implement instanceof in JavaScript". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.