前言
- 360极速浏览器: 12.0.1476.0 (正式版本) (32 位)
- 修订版本: 47c49e5a5935cfffa348dcfa6c581f4394327ac8
- 操作系统: Windows 10 OS Version 2004 (Build 19041.450)
- JavaScript :V8 7.8.279.23
注:使用命令chrome://version/
查看 JavaScript 版本
javascript的数据类型
Javascript的数据类型有:
- Number 数字类型
- String 字符串类型
- Boolean 布尔类型
- Function 函数
- Undefined 没有定义类型
- Object 对象
console.log(typeof 1); // number
console.log(typeof 3.14); // number
console.log(typeof 'abc'); // string
console.log(typeof "123"); // string
console.log(typeof true); // boolean
console.log(typeof undefined); // undefined
console.log(typeof function(){}); // function
console.log(typeof [1,2,3]); // object
console.log(typeof new Array()); // object
console.log(typeof new Date()); // object
console.log(typeof null); // object
console.log(typeof new Map()); // object
- null 值是 object 类型
查看类型
用 typeof
无法区分 Object类型中的 Array、Date、Map等类型。
使用Object.prototype.toString
可以解决此问题。
console.log(Object.prototype.toString.call(1)); //[object Number]
console.log(Object.prototype.toString.call(3.14)); //[object Number]
console.log(Object.prototype.toString.call('a')); //[object String]
console.log(Object.prototype.toString.call(true)); //[object Boolean]
console.log(Object.prototype.toString.call([1,2,3])); //[object Array]
console.log(Object.prototype.toString.call(null)); //[object Null]
console.log(Object.prototype.toString.call(undefined)); //[object Undefined]
console.log(Object.prototype.toString.call(function(){})); //[object Function]
console.log(Object.prototype.toString.call(new Array())); //[object Array]
console.log(Object.prototype.toString.call(Date)); //[object Function]
console.log(Object.prototype.toString.call(new Date())); //[object Date]
console.log(Object.prototype.toString.call(new Date().getYear())); //[object Number]
console.log(Object.prototype.toString.call(new Date().getYear)); //[object Function]
console.log(Object.prototype.toString.call({name:'zhangsan', age:18})); //[object Object]
console.log(Object.prototype.toString.call({name:'zhangsan', age:18}.name)); //[object String]
console.log(Object.prototype.toString.call(navigator)); //[object Navigator]
参考
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/toString
https://www.cnblogs.com/flyjs/archive/2012/02/20/2360504.html
https://www.jianshu.com/p/67e914ed9307
https://www.ucloud.cn/yun/102213.html
https://blog.csdn.net/qzw752890913/article/details/105553294
https://www.cnblogs.com/chenhongshuang/p/8762226.html