Roshin's blog Roshin's blog
首页
  • 《数据结构与算法》笔记
  • 手写代码
  • 实用方法
  • 文档教程
  • 面试集锦
  • 分类
  • 标签
  • 归档
  • 个人介绍
  • 关于博客
  • 友情链接
GitHub (opens new window)

Roshin

如果你只做自己能力范围之内的事情,就永远没法进步。
首页
  • 《数据结构与算法》笔记
  • 手写代码
  • 实用方法
  • 文档教程
  • 面试集锦
  • 分类
  • 标签
  • 归档
  • 个人介绍
  • 关于博客
  • 友情链接
GitHub (opens new window)
  • 手写代码

    • Array.prototype.map()
    • Array.prototype.reduce()
    • Array.prototype.flat()
    • instanceof 运算符
      • instanceof
      • 语法
      • 参数
      • 示例
      • 手写实现
  • 实用方法

  • JavaScript
  • 手写代码
roshin
2021-04-13

instanceof 运算符

instanceof #

instanceof 运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上。

语法 #

object instanceof constructor;
1

参数 #

  • object: 某个实例对象
  • constructor: 某个构造函数

示例 #

function Car(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}
const auto = new Car('Honda', 'Accord', 1998);

console.log(auto instanceof Car); // true
console.log(auto instanceof Object); // true
1
2
3
4
5
6
7
8

手写实现 #

function instabceof(target, origin) {
  while (target) {
    if (Object.getPrototypeOf(target) === origin.prototype) return true;
    target = Object.getPrototypeOf(target);
  }
  return false;
}

console.log(instabceof(auto, Car)); // true
console.log(instabceof(auto, Object)); // true
1
2
3
4
5
6
7
8
9
编辑 (opens new window)
上次更新: 2021-06-23 08:43:55
Array.prototype.flat()
函数柯里化

← Array.prototype.flat() 函数柯里化→

最近更新
01
函数柯里化
04-15
02
Array.prototype.flat()
04-14
03
Array.prototype.reduce()
04-14
更多文章>
Theme by Vdoing Copyright © 2021-present Roshin | MIT License | 粤ICP备18116277号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式