Home Web Front-end JS Tutorial 理解Javascript_07_理解instanceof实现原理_javascript技巧

理解Javascript_07_理解instanceof实现原理_javascript技巧

May 16, 2016 pm 06:18 PM
instanceof Implementation principle

理解Javascript_07_理解instanceof实现原理_javascript技巧
那么instanceof的这种行为到底是如何实现的呢,现在让我们揭开instanceof背后的迷雾。

instanceof原理
照惯例,我们先来看一段代码:
复制代码 代码如下:

function Cat(){}
Cat.prototype = {}
function Dog(){}
Dog.prototype ={}
var dog1 = new Dog();
alert(dog1 instanceof Dog);//true
alert(dog1 instanceof Object);//true
Dog.prototype = Cat.prototype;
alert(dog1 instanceof Dog);//false
alert(dog1 instanceof Cat);//false
alert(dog1 instanceof Object);//true;
var dog2= new Dog();
alert(dog2 instanceof Dog);//true
alert(dog2 instanceof Cat);//true
alert(dog2 instanceof Object);//true
Dog.prototype = null;
var dog3 = new Dog();
alert(dog3 instanceof Cat);//false
alert(dog3 instanceof Object);//true
alert(dog3 instanceof Dog);//error

让我们画一张内存图来分析一下:
理解Javascript_07_理解instanceof实现原理_javascript技巧
内存图比较复杂,解释一下:
程序本身是一个动态的概念,随着程序的执行,Dog.prototype会不断的改变。但是为了方便,我只画了一张图来表达这三次prototype引用的改变。在堆中,右边是函数对象的内存表示,中间的是函数对象的prototype属性的指向,左边的是函数对象创建的对象实例。其中函数对象指向prototype属性的指针上写了dog1,dog2,dog3分别对应Dog.prototype的三次引用改变。它们和栈中的dog1,dog2,dog3也有对应的关系。(注:关于函数对象将在后续博文中讲解)
来有一点要注意,就是dog3中函数对象的prototype属性为null,则函数对象实例dog3的内部[[prototype]]属性将指向Object.prototype,这一点在《理解Javascript_06_理解对象的创建过程》已经讲解过了。

结论
根据代码运行结果和内存结构,推导出结论:
instanceof 检测一个对象A是不是另一个对象B的实例的原理是:查看对象B的prototype指向的对象是否在对象A的[[prototype]]链上。如果在,则返回true,如果不在则返回false。不过有一个特殊的情况,当对象B的prototype为null将会报错(类似于空指针异常)。

这里推荐一篇文章,来自于岁月如歌,也是关于instanceof原理的,角度不同,但有异曲同工之妙。
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

In-depth understanding of the underlying implementation mechanism of Kafka message queue In-depth understanding of the underlying implementation mechanism of Kafka message queue Feb 01, 2024 am 08:15 AM

Overview of the underlying implementation principles of Kafka message queue Kafka is a distributed, scalable message queue system that can handle large amounts of data and has high throughput and low latency. Kafka was originally developed by LinkedIn and is now a top-level project of the Apache Software Foundation. Architecture Kafka is a distributed system consisting of multiple servers. Each server is called a node, and each node is an independent process. Nodes are connected through a network to form a cluster. K

What does instanceof do? What does instanceof do? Nov 14, 2023 pm 03:50 PM

The function of instanceof is to determine whether an object is an instance of a certain class, or whether it implements a certain interface. instanceof is an operator used to check whether an object is of a specified type. Usage scenarios of instanceof operator: 1. Type checking: can be used to determine the specific type of an object, so as to perform different logic according to different types; 2. Interface judgment: can be used to determine whether an object implements an interface, so as to determine whether an object implements an interface. The definition of the interface calls the corresponding method; 3. Downward transformation, etc.

Detailed explanation of the operating mechanism and implementation principles of PHP core Detailed explanation of the operating mechanism and implementation principles of PHP core Nov 08, 2023 pm 01:15 PM

PHP is a popular open source server-side scripting language that is heavily used for web development. It can handle dynamic data and control HTML output, but how to achieve this? Then, this article will introduce the core operating mechanism and implementation principles of PHP, and use specific code examples to further illustrate its operating process. PHP source code interpretation PHP source code is a program written in C language. After compilation, it generates the executable file php.exe. For PHP used in Web development, it is generally executed through A

Implementation principle of particle swarm algorithm in PHP Implementation principle of particle swarm algorithm in PHP Jul 10, 2023 pm 11:03 PM

Principle of Particle Swarm Optimization Implementation in PHP Particle Swarm Optimization (PSO) is an optimization algorithm often used to solve complex nonlinear problems. It simulates the foraging behavior of a flock of birds to find the optimal solution. In PHP, we can use the PSO algorithm to quickly solve problems. This article will introduce its implementation principle and give corresponding code examples. Basic Principle of Particle Swarm Optimization The basic principle of particle swarm algorithm is to find the optimal solution through iterative search. There is a group of particles in the algorithm

In-depth analysis of the technical principles and applicable scenarios of Kafka message queue In-depth analysis of the technical principles and applicable scenarios of Kafka message queue Feb 01, 2024 am 08:34 AM

The implementation principle of Kafka message queue Kafka is a distributed publish-subscribe messaging system that can handle large amounts of data and has high reliability and scalability. The implementation principle of Kafka is as follows: 1. Topics and partitions Data in Kafka is stored in topics, and each topic can be divided into multiple partitions. A partition is the smallest storage unit in Kafka, which is an ordered, immutable log file. Producers write data to topics, and consumers read from

Analyze the implementation principle of swoole's asynchronous task processing function Analyze the implementation principle of swoole's asynchronous task processing function Aug 05, 2023 pm 04:15 PM

Analyze the implementation principle of swoole's asynchronous task processing function. With the rapid development of Internet technology, the processing of various problems has become more and more complex. In web development, handling a large number of requests and tasks is a common challenge. The traditional synchronous blocking method cannot meet the needs of high concurrency, so asynchronous task processing becomes a solution. As a PHP coroutine network framework, Swoole provides powerful asynchronous task processing functions. This article will use a simple example to analyze its implementation principle. Before we start, we need to make sure we have

Master the underlying working mechanism of Tomcat middleware Master the underlying working mechanism of Tomcat middleware Dec 28, 2023 pm 05:25 PM

To understand the underlying implementation principles of Tomcat middleware, you need specific code examples. Tomcat is an open source, widely used Java Web server and Servlet container. It is highly scalable and flexible and is commonly used to deploy and run Java Web applications. In order to better understand the underlying implementation principles of Tomcat middleware, we need to explore its core components and operating mechanism. This article will analyze the underlying implementation principles of Tomcat middleware through specific code examples. Tom

The principle of Java crawler technology: detailed analysis of the web page data crawling process The principle of Java crawler technology: detailed analysis of the web page data crawling process Jan 09, 2024 pm 02:46 PM

In-depth analysis of Java crawler technology: Implementation principles of web page data crawling Introduction: With the rapid development of the Internet and the explosive growth of information, a large amount of data is stored on various web pages. These web page data are very important for us to carry out information extraction, data analysis and business development. Java crawler technology is a commonly used method of web page data crawling. This article will provide an in-depth analysis of the implementation principles of Java crawler technology and provide specific code examples. 1. What is crawler technology? Crawler technology (WebCrawling) is also called web crawler technology.

See all articles