JavaScript/instanceof:修订间差异
来自康健生活
无编辑摘要 |
|||
| 第41行: | 第41行: | ||
function bar(){} | function bar(){} | ||
bar instanceof Object; | bar instanceof Object; | ||
class Puz{}; | |||
Puz instanceof Object; | |||
Puz instanceof Function; | |||
1n instanceof BigInt; | |||
BigInt(1) instanceof BigInt; | |||
</syntaxhighlight>|{{consoleout|<pre> | </syntaxhighlight>|{{consoleout|<pre> | ||
true | true | ||
| 第48行: | 第53行: | ||
true | true | ||
true | true | ||
true | |||
true | |||
true | |||
false | |||
true | true | ||
</pre>}}}} | </pre>}}}} | ||
== 原理 == | |||
== 模拟 instanceof == | == 模拟 instanceof == | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
2020年6月26日 (五) 06:27的版本
引言
继承对象
原始类型
构造函数
原生类型
原理
模拟 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
No categories