<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
/*
instanceof运算符 : 对象 instanceof 构造函数
作用 : 检测 右边 构造函数.prototype 在不在 左边 对象的原型链中
*/
//1.
/*
arr instanceof Array : 检测Array.prototype 在不在 arr的原型链中
数组原型链 : arr -> Array.prototype -> Object.prototype ->null
*/
var arr = [10,20,30];
console.log(arr instanceof Array);//true
console.log(arr instanceof Object);//true
//2.
/*
根据instanceof运算规则, 左边Function是对象, 右边Function是构造函数
Function函数对象的原型链 : Function ->Function.prototype ->Object.prototype->null
*/
console.log(Function instanceof Function);//true
console.log(Function instanceof Object);//true
//3.
/*
根据instanceof运算规则, 左边Object是对象, 右边Object是构造函数
Object函数对象原型链 : Object -> Function.prototype -> Object.prototype ->null
*/
console.log(Object instanceof Function);//true
console.log(Object instanceof Object);//true
</script>
</body>
</html>
06- instanceof运算符工作原理
最新推荐文章于 2023-12-12 10:01:10 发布