Table of Contents
Preliminary knowledge
Famous Symbols
InstanceofOperator(O, C)
OrdinaryHasInstance(C, O)
Function.prototype[@@hasInstance](V)
Example
Home Web Front-end JS Tutorial Detailed explanation of the instanceof operator in the ECMAScript7 specification (with examples)

Detailed explanation of the instanceof operator in the ECMAScript7 specification (with examples)

Sep 17, 2018 pm 02:01 PM
javascript

This article brings you a detailed explanation of the instanceof operator (with examples) in the ECMAScript7 specification. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

This article mainly explains the instanceof operator in the ECMAScript7 specification.

Preliminary knowledge

Famous Symbols

"Famous" Symbols refer to built-in symbols, which are defined on the Symbol object. ECMAScript7 uses the form of @@name to reference these built-in symbols. For example, @@hasInstance mentioned below is actually Symbol.hasInstance.

InstanceofOperator(O, C)

O instanceof C will internally call the InstanceofOperator(O, C) abstract operation. The steps of this abstract operation are as follows:

If C The data type is not an object, and a type error exception is thrown;

Let instOfHandler equal GetMethod(C, @@hasInstance), and the approximate semantics is to get the value of the @@hasInstance attribute of object C;

If the value of instOfHandler is not undefined, then:

Returns the result of ToBoolean(? Call(instOfHandler, C, « O »)). The approximate semantics is to execute instOfHandler(O), and then force the call result to Boolean type return.

If C cannot be called, throw an exception of type error;

Return the result of OrdinaryHasInstance(C, O).

OrdinaryHasInstance(C, O)

The steps of OrdinaryHasInstance(C, O) abstract operation are as follows:

If C cannot be called, return false;

If C has an internal slot [[BoundTargetFunction]], then:

Let BC equal the value of C's internal slot [[BoundTargetFunction]];

Return InstanceofOperator(O, BC) The result;

If the type of O is not an object, return false;

Let P equal Get(C, "prototype"), the approximate semantics is to get the value of C.prototype;

If the data type of P is not an object, throw a type error exception;

Repeat the following steps:

Let O equal O.[[GetPrototypeOf]]() As a result, the approximate semantics is to obtain the prototype object of O;

If O is equal to null, return false;

If the result of SameValue(P, O) is true, return true.

SameValue abstract operations refer to ==, === in JavaScript and Object.is() in Object.js(). Object.is() uses the result of this abstract operation.

It can be seen from the above step 2 that if C is a bind function, the InstanceofOperator(O, BC) operation will be re-executed on the target function bound by C.

As can be seen from step 6 above, the prototype object of object O will be repeatedly obtained, and then the prototype object and the prototype attribute of C are compared to see if they are equal, until true is returned when equal, or O becomes null, that is, the traversal is completed. The entire prototype chain returns false.

Function.prototype[@@hasInstance](V)

It can be known from steps 2 and 3 of the above InstanceofOperator(O, C) abstract operation that if C is defined above or inherits @ @hasInstance attribute, the value of the attribute will be called without going to steps 4 and 5. The purpose of steps 4 and 5 is to be compatible with browsers that do not implement the @@hasInstance method. If a function does not define or inherit the @@hasInstance attribute, then the default instanceof semantics will be used, which is the steps described by the OrdinaryHasInstance(C, O) abstract operation.

In the ECMAScript7 specification, the @@hasInstance attribute is defined on the prototype attribute of Function. The steps of Function.prototype[@@hasInstance](V) are as follows:

Let F equal this value;

Return the result of OrdinaryHasInstance(F, V).

So, you can see that by default, the semantics of instanceof are the same, and they all return the result of OrdinaryHasInstance(F, V). Why does it say by default? Because you can override the Function.prototype[@@hasInstance] method to customize the behavior of instanceof.

Example

function A () {}
function B () {}

var a = new A
a.__proto__ === A.prototype // true
a.__proto__.__proto__ === Object.prototype // true
a.__proto__.__proto__.__proto__ === null // true

a instanceof A // true
a instanceof B // false
Copy after login

It can be seen from step 6 of OrdinaryHasInstance(C, O):

For an instanceof A, P is A.prototype, in the first cycle At this time, the prototype object a._proto__ of a is A.prototype, that is, O in the step is A.prototype, so true is returned;

For a instanceof B, P is B.prototype, in the During the first loop, the prototype object a._proto__ of a is A.prototype, not equal to P; execute the second loop, at this time O is a.__proto__.__proto__, which is Object.prototype, not equal to P; execute The third loop, at this time O is a.__proto__.__proto__.__proto__, which is null, that is, the prototype chain has been traversed, so false is returned.

Follow the above example:

A.prototype.__proto__ = B.prototype

a.__proto__ === A.prototype // true
a.__proto__.__proto__ === B.prototype // true
a.__proto__.__proto__.__proto__ === Object.prototype // true
a.__proto__.__proto__.__proto__.__proto__ === null // true

a instanceof B // true
Copy after login

In the above example, we set B.prototype to be a link in the prototype chain of a, so that a instanceof B is in OrdinaryHasInstance(C, In the second loop of step 6 of O), true was returned.

From the second step of OrdinaryHasInstance(C, O), we know that the behavior of the bind function is different from the behavior of the ordinary function:

function A () {}
var B = A.bind()

B.prototype === undefined // true

var b = new B
b instanceof B // true
b instanceof A // true
Copy after login

As can be seen from the above example, B.prototype is undefined. Therefore, the return result of instanceof acting on the bind function is actually the return value acting on the bound target function, which basically has nothing to do with the bind function.

As can be seen from steps 2 and 3 of InstanceofOperator(O, C), we can customize the behavior of instanceof by overriding the @@hasInstance method on the prototype:

function A () {}
var a = new A
a instanceof A // true

A[Symbol.hasInstance] = function () { return false }
a instanceof A // ?
Copy after login

在chrome浏览器测试了一下,发现还是输出true。然后看了一下ECMAScript6的文档,
ECMAScript6文档里面还没有规定可以通过@@hasInstance改变instanceof的行为,所以应该是目前chrome浏览器还没有实现ECMAScript7中的instanceof操作符的行为。

总结

本文主要讲解ECMAScript7规范中的instanceof操作符,希望大家能有所收获。

The above is the detailed content of Detailed explanation of the instanceof operator in the ECMAScript7 specification (with examples). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript and WebSocket: Building an efficient real-time image processing system JavaScript and WebSocket: Building an efficient real-time image processing system Dec 17, 2023 am 08:41 AM

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data

See all articles