In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the function of isPrototypeOf function in JavaScript". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the function of isPrototypeOf in JavaScript"?
1. IsPrototypeOf ()
IsPrototypeOf () is a method under the Object function (class) that determines whether the current object is the prototype of another object, and returns true if so, false otherwise.
The key to understanding this function is on the prototype chain, which is said to be one of the three mountains of JavaScript.
We will not elaborate on the principle here, but to put it simply, there are three points:
1. Function objects are born with a prototype prototype property.
two。 Each object is also born with an attribute _ _ proto__ that points to the prototype of the function object that generated it.
3. The prototype of the function object also has _ _ proto__ pointing to the prototype of the function object that generated it.
Example 1: let o = new Object (); console.log (Object.prototype.isPrototypeOf (o)); / / true
Because the o object is an instance of Object, the prototype of the o object (_ _ proto__) points to the prototype of Object (prototype), which outputs true.
Example 2, define the Human class by yourself: function Human () {} let human = new Human (); console.log (Human.prototype.isPrototypeOf (human)); / / true
This example is similar to the previous example, because the human object is an instance of Human, so the prototype of the human object (_ _ proto__) points to the prototype of Human (prototype), which outputs true.
Example 3, let's see whether the Object prototype (prototype) is the prototype of human: console.log (Object.prototype.isPrototypeOf (human)); / / true
Why? It may be easier to explain in code, see the following derivation
/ because the prototype (_ _ proto__) in the Human prototype (prototype) points to the Object prototype (prototype) Human.prototype.__proto__ = Object.prototype//, and because the human prototype (_ _ proto__) points to the Human prototype (prototype) huamn.__proto__ = Human.prototype//, the human object prototype (_ _ proto__) prototype (_ _ proto__) points to the Object prototype (prototype) Huamn.__proto__.__proto__ = Object.prototype
If you look at the structure of human, it's easy to understand:
Is the prototype of Object the prototype of the human object? To be exact, the prototype of Object (prototype) is on the prototype chain of human.
Example 4 whether the prototype is the prototype of the built-in class:
Since the built-in classes Number, String, Boolean, Function and Array in JavaScript inherit Object, the following output is also true:
Console.log (Object.prototype.isPrototypeOf (Number)); / trueconsole.log (Object.prototype.isPrototypeOf (String)); / / trueconsole.log (Object.prototype.isPrototypeOf (Boolean)); / / trueconsole.log (Object.prototype.isPrototypeOf (Array)); / / trueconsole.log (Object.prototype.isPrototypeOf (Function)); / / true
Natural Object.prototype is also the prototype of instances of Number, String, Boolean, Function, and Array.
Example 5 minute object is also a function (class):
It is also worth mentioning that Function.prototype is also the prototype of Object, because Object is also a function (class) and they are generated from each other.
Take a look at the following output:
Console.log (Object.prototype.isPrototypeOf (Function)); / / true
Console.log (Function.prototype.isPrototypeOf (Object)); / / true
2. The difference between instanceof and
Instanceof is an instance used to determine whether an object belongs to an object.
For example:
Function Human () {} let human = new Human (); / / human is an instance of Human, so the result output trueconsole.log (human instanceof Human); / / true / / because all classes inherit Object, the result also outputs trueconsole.log (human instanceof Object); / / true / / because the human object is not an array, the result output falseconsole.log (human instanceof Array); / / false
Let's take some examples of built-in classes:
/ / [1Jing 2jue 3] is an instance of Array, so output trueconsole.log ([1je 2je 3] instanceof Array); / / true / / method function () {} is an instance of Function, so output trueconsole.log (function () {} instanceof Function)
The principle of instanceof is to judge whether the prototype object (prototype) of the class can be found in the prototype chain of the instance, and isPrototypeOf is to judge whether the prototype object (prototype) of the class is on the prototype chain of the instance.
Therefore, as far as I understand it, the two expressions have the same meaning, that is, they are written differently, and the following two outputs should be the same:
Console.log (An instanceof B); console.log (B.prototype.isPrototypeOf (A)); at this point, I believe you have a deeper understanding of "what is the function of the isPrototypeOf function in JavaScript". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.