<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>判断数组的方式</title>
</head>
<body>
<script>
let arr = [1,2,3,4,5,6,7,8]
let object = {
name: 'Rebecca',
arr: [1,2,3,4,5,6,7,8]
}
let str = '判断数组的方式--我是字符串'
// 方法一:Object.prototype.toString.call()
let result1 = Object.prototype.toString.call(arr)
let result2 = Object.prototype.toString.call(object)
let result3 = Object.prototype.toString.call(str)
console.log('result1:', result1) // [object Array]
console.log('result2:', result2) // [object Object]
console.log('result3:', result3) // [object String]
// 方法二:通过原型链做判断
let result4 = arr.__proto__ === Array.prototype
let result5 = object.__proto__ === Array.prototype
let result6 = str.__proto__ === Array.prototype
console.log('result4:', result4) // true
console.log('result5:', result5) // false
console.log('result6:', result6) // false
// 通过ES6中的Array.isArray()
let result7 = Array.isArray(arr)
let result8 = Array.isArray(object)
let result9 = Array.isArray(str)
console.log('result7:', result7) // true
console.log('result8:', result8) // false
console.log('result9:', result9) // false
// 方法四:intanceof()
let resultF = arr instanceof Array
let resultS = object instanceof Array
let resultT = str instanceof Array
console.log('resultF:', resultF) // true
console.log('resultS:', resultS) // false
console.log('resultT:', resultT) // false
</script>
</body>
</html>