类型判断
数据类型应该算得上是 js 的基础第一课。在日常的开发中会遇到很多类型判断和类型转换的场景。
数据类型
js中的数据类型分为基本类型和引用类型:
- 基本类型
Number、Boolean、undefined、null、BigInt、String、Symbol - 引用类型
Object、Function、Array、RegExp、Date
基本类型和引用类型的不同
基本类型的值是不可变的,引用类型的值是可变的。
在复制时:基本类型复制的是值,引用类型复制的是引用。
在比较时:基本类型只用比较值,引用类型需要同时比较引用地址和值。
数据存储
在 js 中基本类型存储在栈内存中,引用类型存储在堆内存中。这是因为栈的存取速度相当快,仅次于 cpu 中寄存器的存取速度。
类型判断
typeof – 基本类型的判断方法
typeof "s"; // "string"
typeof { x: 1 }; // "object"
typeof [1, 2, 3]; // "object"
复制代码
tips: 关于 typeof 的一个历史 bug
typeof null; // 'object'
复制代码
typeof 方法只能精确判断基本类型,对于引用类型的判断都为 “object”。
instanceof – 引用类型的判断方法
instanceof 用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上。
a instanceof b ,a 是否为 b 的实例。
var a = { x: 1 };
var b = [1, 2, 3];
var c = function () {
console.log("aa");
};
connsole.log(a instanceof Object); // true
console.log(b instanceof Array); // true
console.log(c instanceof Function); // true
复制代码
tips:不能检测 null 和 undefined。
instanceof 方法可以精确判断 引用类型 的数据类型;
constructor
查询对象的构造函数
function getConstructor(obj) {
let a = obj;
return a.constructor;
}
getConstructor(1);
// ƒ Number() { [native code] }
getConstructor("");
// ƒ String() { [native code] }
getConstructor(false);
// ƒ Boolean() { [native code] }
getConstructor(Symbol(1));
// ƒ Symbol() { [native code] }
getConstructor(BigInt(1));
// ƒ BigInt() { [native code] }
getConstructor(function () {});
// ƒ Function() { [native code] }
getConstructor(new Date());
// ƒ Date() { [native code] }
getConstructor(/a/);
// ƒ RegExp() { [native code] }
getConstructor({});
// ƒ Object() { [native code] }
getConstructor([]);
// ƒ Array() { [native code] }
复制代码
原型方法
Object.prototype.toString.call()
Object.prototype.toString.call("xx"); // "[object String]"
Object.prototype.toString.call([12, 3]); // "[object Array]"
Object.prototype.toString.call({ x: 1 }); // "[object Object]"
Object.prototype.toString.call(function () {}); //"[object Function]"
Object.prototype.toString.call(true); // "[object Boolean]"
Object.prototype.toString.call(null); // "[object Null]"
Object.prototype.toString.call(1); // "[object Number]"
Object.prototype.toString.call(undefined); // "[object Undefined]"
Object.prototype.toString.call(new Date()); // "[object Date]"
Object.prototype.toString.call(new RegExp()); // "[object RegExp]"
Object.prototype.toString.call(BigInt(2)); // "[object BigInt]"
复制代码
- 总结
js 中数据类型的判断方法,typeof 常被用于检测基本类型,instanceof 常被用于检测引用类型, Object.prototype.toString 这种方法算是通用方法,实际开发中也是我最喜欢使用的。
类型转换
以下场景会触发类型转换:
- if 条件语句
- 三目表达式等逻辑语句
- ==、+、-、*等运算算符
开发中值得注意的点
- Number有最大限制:
// 通过Number.MAX_VALUE访问,超过此值计算时会丢失精度
Number.MAX_VALUE // 1.7976931348623157e+308
复制代码
- 二进制转十进制:
parseInt('1010', 2) // 10
parseInt('a', 12) // 10
复制代码
- 十进制转二进制:
(10).toString(2) // "1010"
(10).toString(12) // "a"
复制代码
哇哦,奇妙的知识又增加了!姐妹们get起来吧!
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END




















![[桜井宁宁]COS和泉纱雾超可爱写真福利集-一一网](https://www.proyy.com/skycj/data/images/2020-12-13/4d3cf227a85d7e79f5d6b4efb6bde3e8.jpg)

![[桜井宁宁] 爆乳奶牛少女cos写真-一一网](https://www.proyy.com/skycj/data/images/2020-12-13/d40483e126fcf567894e89c65eaca655.jpg)