JavaScript判断数组的几种方式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
const ary = [1,2,3,4]
console.log(ary instanceof Array)
console.log(Array.isArray(ary))
console.log(ary.constructor.name === 'Array')
console.log(ary.__proto__ === Array.prototype)
console.log(Object.prototype.toString.call(ary) === '[object Array]')
</script>
</body>
</html>
instanceof 返回一个布尔值,判断对象是否为某个构造函数的实例
Array.isArray(),ES6新增判断是否为数组
constructor.name 返回对象构造函数的name