查看“︁JavaScript/instanceof”︁的源代码
来自康健生活
←
JavaScript/instanceof
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
== 引言 == == 继承对象 == {{leftrightflex|<syntaxhighlight lang="javascript"> class People{}; class Student extends People{}; new Student() instanceof Student; new Student() instanceof People; </syntaxhighlight>|{{consoleout|<pre> true true </pre>}}}} == 原始类型 == {{leftrightflex|<syntaxhighlight lang="javascript"> 1 instanceof Number; true instanceof Boolean; "str" instanceof String; </syntaxhighlight>|{{consoleout|<pre> false false false </pre>}}}} == 构造函数 == {{leftrightflex|<syntaxhighlight lang="javascript"> new Number(1) instanceof Number; new Boolean(true) instanceof Boolean; new String('str') instanceof String; </syntaxhighlight>|{{consoleout|<pre> true true true </pre>}}}} == 原生类型 == {{leftrightflex|<syntaxhighlight lang="javascript"> [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; </syntaxhighlight>|{{consoleout|<pre> true true true true true true true true true false true </pre>}}}} == 原理 == == 模拟 instanceof == <syntaxhighlight lang="javascript"> 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</syntaxhighlight>
此页面嵌入的页面:
模板:Consoleout
(
查看源代码
)
模板:Double-columns
(
查看源代码
)
模板:Double-columns/style.css
(
查看源代码
)
返回
JavaScript/instanceof
。
导航菜单
康健生活
搜索
user-page
通知
user-interface-preferences
个人工具
登录
五一
多读书,多运动,多到远处走走,再看看不一样的世界。