Sometimes when you look at some framework source code, you will encounter 1. isPrototypeOf() The key to understanding this function is on the prototype chain, which is said to be one of the three major mountains of The principles are not described in detail here, but simply put, there are three points:
Example 1, Object class instance:let o = new Object(); console.log(Object.prototype.isPrototypeOf(o)); // true Because the Example 2: Define the Human class yourself:function Human() {} let human = new Human(); console.log(Human.prototype.isPrototypeOf(human)); // true This example is similar to the previous one. Because the Example 3: Let’s see if Object’s prototype is the prototype of human:console.log(Object.prototype.isPrototypeOf(human)); // true Why? , which may be better explained with code, please see the following derivation: // Because the prototype (__proto__) in Human's prototype (prototype) points to Object's prototype (prototype) Human.prototype.__proto__ === Object.prototype // And because the prototype of human (__proto__) points to the prototype of Human (prototype) huamn.__proto__ === Human.prototype // So the prototype of the human object (__proto__) points to the prototype of Object (prototype) huamn.__proto__.__proto__ === Object.prototype This is easy to understand if you look at the structure of human: So is Example 4, whether Object.prototype is the prototype of a built-in class: The built-in classes console.log(Object.prototype.isPrototypeOf(Number)); // true console.log(Object.prototype.isPrototypeOf(String)); // true console.log(Object.prototype.isPrototypeOf(Boolean)); // true console.log(Object.prototype.isPrototypeOf(Array)); // true console.log(Object.prototype.isPrototypeOf(Function)); // true Naturally, Example 5, Object is also a function (class): It is also worth mentioning that Please see the following output:
2. Difference from instanceof For example: function Human() {} let human = new Human(); // human is an instance of Human, so the output is true console.log(human instanceof Human); // true // Because all classes inherit Object, the result also outputs true console.log(human instanceof Object); // true // Because the human object is not an array, the result output is false console.log(human instanceof Array); // false Here are some more examples of built-in classes: // [1,2,3] is an instance of Array, so the output is true console.log([1, 2, 3] instanceof Array); // true // The method function(){} is an instance of Function, so it outputs true console.log(function(){} instanceof Function); The principle of So my understanding is that the meaning of these two expressions is the same, but they are written differently. The following two outputs should be the same: console.log(A instanceof B); console.log(B.prototype.isPrototypeOf(A)); summary In fact, to understand the This is the end of this article about the You may also be interested in:
|
<<: TinyEditor is a simple and easy-to-use HTML WYSIWYG editor
>>: A brief summary of basic web page performance optimization rules
The previous article introduced the implementatio...
React Native can develop iOS and Android native a...
As shown below: As shown above, just replace it. ...
Error message: ERROR 2002: Can't connect to l...
Download: http://dev.mysql.com/downloads/mysql/ U...
1. One-stop solution 1. Problem analysis and loca...
Table of contents illustrate 1. Blob object 2. Fr...
This article shares the specific implementation c...
When making a website, I found that video files, s...
As a super rookie, I just started learning MySQL ...
WeChat applet trajectory playback mainly uses pol...
In order to speed up the parsing of the website, ...
Seurat is a heavyweight R package for single-cell...
Docker installation Install dependency packages s...
CSS: Copy code The code is as follows: html,body{ ...