JavaScript/instanceof:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
无编辑摘要
 
(未显示同一用户的3个中间版本)
第6行: 第6行:


== 继承对象 ==
== 继承对象 ==
{{leftrightflex|<syntaxhighlight lang="javascript">
{{Double-columns|<syntaxhighlight lang="javascript">
class People{};
class People{};
class Student extends People{};
class Student extends People{};
第16行: 第16行:
</pre>}}}}
</pre>}}}}
== 原始类型 ==
== 原始类型 ==
{{leftrightflex|<syntaxhighlight lang="javascript">
{{Double-columns|<syntaxhighlight lang="javascript">
1 instanceof Number;
1 instanceof Number;
true instanceof Boolean;
true instanceof Boolean;
第26行: 第26行:
</pre>}}}}
</pre>}}}}
== 构造函数 ==
== 构造函数 ==
{{leftrightflex|<syntaxhighlight lang="javascript">
{{Double-columns|<syntaxhighlight lang="javascript">
new Number(1) instanceof Number;
new Number(1) instanceof Number;
new Boolean(true) instanceof Boolean;
new Boolean(true) instanceof Boolean;
第36行: 第36行:
</pre>}}}}
</pre>}}}}
== 原生类型 ==
== 原生类型 ==
{{leftrightflex|<syntaxhighlight lang="javascript">
{{Double-columns|<syntaxhighlight lang="javascript">
[1,2,3] instanceof Array;
[1,2,3] instanceof Array;
/abc/ instanceof RegExp;
/abc/ instanceof RegExp;
第51行: 第51行:
1n instanceof BigInt;
1n instanceof BigInt;
BigInt(1) instanceof BigInt;
BigInt(1) instanceof BigInt;
new Map() instanceof Map;
new Set() instanceof Set;
var puz = new Promise((resolve,reject)=>{});
puz instanceof Promise;
var xyz = new Proxy({},{});
xyz instanceof Proxy;
</syntaxhighlight>|{{consoleout|<pre>
</syntaxhighlight>|{{consoleout|<pre>
true
true
第62行: 第68行:
true
true
false
false
true
true
true
true
VM8385:2 Uncaught TypeError: Function has non-object prototype 'undefined' in instanceof check
    at Function.[Symbol.hasInstance] (<anonymous>)
    at <anonymous>:2:5
</pre>}}}}
== 复杂用法 ==
{{Double-columns|<syntaxhighlight lang="javascript">
function Foo(){}
Object instanceof Object;
Function instanceof Function;
Number instanceof Number;
String instanceof String;
Function instanceof Object;
Foo instanceof Function;
Foo instanceof Foo;
</syntaxhighlight>|{{consoleout|<pre>
true
true
false
false
true
true
false
</pre>}}}}
== 继承 ==
{{Double-columns|<syntaxhighlight lang="javascript">
function Aoo(){}
function Foo(){}
Foo.prototype = new Aoo();
var foo = new Foo();
console.log(foo instanceof Foo);
console.log(foo instanceof Aoo);
</syntaxhighlight>|{{consoleout|<pre>true
true
true
</pre>}}}}
</pre>}}}}
== 原理 ==
== 原理 ==



2026年11月26日 (四) 05:07的最新版本

引言

  • 对于对象能返回构造对象的构造函数名。
  • 能正确判断继承的对象。
  • 对于原始类型字面量无法正确判断(Number,Boolean,String)。
  • 对 new 构造的对象可以正确判断,可以正确判断原生类型(RegExp,Array,Function,Object)。

继承对象

class People{};
class Student extends People{};
new Student() instanceof Student;
new Student() instanceof People;
true
true

原始类型

1 instanceof Number;
true instanceof Boolean;
"str" instanceof String;
false
false
false

构造函数

new Number(1) instanceof Number;
new Boolean(true) instanceof Boolean;
new String('str') instanceof String;
true
true
true

原生类型

[1,2,3] instanceof Array;
/abc/ instanceof RegExp;
({}) instanceof Object;
function foo(){}
foo instanceof Function;
[1,2,3] instanceof Object;
/abc/ instanceof Object;
function bar(){}
bar instanceof Object;
class Puz{};
Puz instanceof Object;
Puz instanceof Function;
1n instanceof BigInt;
BigInt(1) instanceof BigInt;
new Map() instanceof Map;
new Set() instanceof Set;
var puz = new Promise((resolve,reject)=>{});
puz instanceof Promise;
var xyz = new Proxy({},{});
xyz instanceof Proxy;
true
true
true
true
true
true
true
true
true
false
true
true
true
true
VM8385:2 Uncaught TypeError: Function has non-object prototype 'undefined' in instanceof check
    at Function.[Symbol.hasInstance] (<anonymous>)
    at <anonymous>:2:5

复杂用法

function Foo(){} 
Object instanceof Object; 
Function instanceof Function; 
Number instanceof Number; 
String instanceof String; 
Function instanceof Object; 
Foo instanceof Function; 
Foo instanceof Foo;
true 
true 
false 
false 
true 
true 
false

继承

function Aoo(){} 
function Foo(){} 
Foo.prototype = new Aoo();
var foo = new Foo(); 
console.log(foo instanceof Foo);
console.log(foo instanceof Aoo);
true
true

原理

模拟 instanceof

function simulateInstanceof (cloneObject, nativeObject) {
  let CO_proto = cloneObject.__proto__;
  let NO_prototype = nativeObject.prototype;
  while(true) {
    if(CO_proto == null) {
      return false;
    }
    if(CO_proto == NO_prototype) {
      return true;
    }
    CO_proto =CO_proto.__proto__;
  }
}
let arr = new Array();
console.log(simulateInstanceof(arr,Array));
// ==> true