准备数据类型
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
</html>
<script>
let a = "string";
let b = 111;
let c = {};
let d = [1, 2, 3];
let e = function () {
console.log("eee");
}
let f = undefined;
let g = null;
let h = new Date();
let i = /test/;
let j = true;
</script>
用typeof()判断()
console.log(typeof(a),typeof(b),typeof(c),typeof(d),typeof(e),typeof(f),typeof(g),typeof(h),typeof(i),typeof(j))
结果
用instanceof判断
console.log(a instanceof String);//false
console.log(b instanceof Number);//false
console.log(c instanceof Object);//true
console.log(d instanceof Array);//true
console.log(e instanceof Function);//true
console.log(h instanceof Date);//true
console.log(i instanceof RegExp);//true
用constructor判断
console.log(c.constructor===Object);//true
console.log(d.constructor===Array);//true
console.log(e.constructor===Function);//true
console.log(h.constructor=== Date);//true
console.log(i.constructor=== RegExp);//true
用Object.prototype.toString.call()判断
console.log(Object.prototype.toString.call(a) === '[object String]');//true
console.log(Object.prototype.toString.call(b) === '[object Number]');//true
console.log(Object.prototype.toString.call(c) === '[object Object]');//true
console.log(Object.prototype.toString.call(d) === '[object Array]');//true
console.log(Object.prototype.toString.call(e) === '[object Function]');//true
console.log(Object.prototype.toString.call(f) === '[object Undefined]');//true
console.log(Object.prototype.toString.call(g) === '[object Null]');//true
console.log(Object.prototype.toString.call(h) === '[object Date]');//true
console.log(Object.prototype.toString.call(i) === '[object RegExp]');//true