JS基本数据类型有8个:
Number、Boolean、String、Object、Null、Undefined (ES6新增Symbol、BigInt)
Number
强转:
Number(1)//1
Number(-1)//-1
Number(1.8)
Number("1")//1
Number("-1")//-1
Number("1.8")//1.8
Number("s")//NaN
Number(true)//1
Number(false)//0
Number("true")//NaN
Number("false")//NaN
Number([])//0
Number({})//NaN
Boolean
强转:
Boolean(1)//true
Boolean(0)//false
Boolean("1")//true
Boolean("0")//true
Boolean([])//true
Boolean({})//true
String
被引号包裹的都是字符串,不管单引号还是双引号。
Object
typeof([])//'object'
typeof({})//'object'
typeof(new Date())//'object'
typeof(Math)//'object'
Undefined和Null
Undefined这个值表示变量不含有值。
可以通过将变量的值设置为 null 来清空变量。
ES6新增
Symbol
Symbol唯一标志符,可用作对象唯一属性名,Symbol数据类型的特点就是唯一性
let id1 = Symbol('id');
let id2 = Symbol('id');
console.log(id1 == id2); //false
Symbol另一特点,隐藏性
let id = Symbol("id");
let obj = {
[id]: 'symbol'
};
for (let option in obj) {
console.log(obj[option]); //无法访问到
}
通过 Object.getOwnPropertySymbols 访问
let id1 = Symbol("id1");
let id2 = Symbol("id2");
let obj = {
wid: 111,
[id1]: 'aaa',
[id2]: 'bbb'
};
let array = Object.getOwnPropertySymbols(obj);
console.log(array); // [ Symbol(id1), Symbol(id2) ]
console.log(obj[array[0]]); // aaa
为解决多次使用同一个symbol的情况
let name1 = Symbol.for('name'); //检测到未创建后新建
let name2 = Symbol.for('name'); //检测到已创建后返回
console.log(name1 === name2); // true
console.log(Symbol.keyFor(name1)); // 'name'
console.log(Symbol.keyFor(name2)); // 'name'
Bigint
BigInt 是一种特殊的数字类型,它提供了对任意长度整数的支持。
创建 bigint 的方式有两种:在一个整数字面量后面加 n 或者调用 BigInt 函数。
console.log(90099999999999992 === 90099999999999993); // true
console.log(90099999999999992n === 90099999999999993n); // false
console.log(BigInt("90099999999999992") === BigInt("90099999999999993")); // false
判断JS数据类型:
(1)typeof
typeof数组会返回Object(数组可以使用Array.isArray()会返回布尔值)
typeof(null)、typeof (new Date())、typeof (new RegExp())、typeof (new Error())都会返回object;
(2)instanceof
功能:A instanceof B 判断A是否是B 的实例
//手写instanceof
//右边变量原型是否存在于左边原型链上
function MyInstanceof(left,right){
let leftValue = left.__proto__;
let rightValue = right.prototype;
while(true){
if(leftValue = null){
return false;
}else if(leftValue == rightValue){
return true;
}else{
leftValue = leftValue.__proto__;
}
}
}
(3)constructor
console.log(new Number(1).constructor == Number);//true
console.log(new String(1).constructor == String);//true
console.log(true.constructor == Boolean);//true
console.log(new Object().constructor == Object);//true
console.log(new Error().constructor == Error);//true
(4)Object.prototype.toString.call()
返回[Object xxx];
对于Object直接调用Object.prototype.toString();
对于其他对象调用时用call()绑定;
console.log(Object.prototype.toString({}));//[object Object]
console.log(Object.prototype.toString.call(''));//[object String]
console.log(Object.prototype.toString.call(1));//[object Number]
console.log(Object.prototype.toString.call(true));//[object Boolean]
console.log(Object.prototype.toString.call(undefined));//[object Undefined]
console.log(Object.prototype.toString.call(null));//[object Null]
console.log(Object.prototype.toString.call(Symbol()));//[object Symbol]
console.log(Object.prototype.toString.call(new Error()));//[object Error]
为什么写Object.prototype.toString.call()
首先,toString要用Object原型上的方法,而Array、function等类型作为Object的实例,都重写了toString()方法,调用的是对应的重写之后的toString方法(Function类型返回内容为函数体的字符串,Array类型返回元素组成的字符串.....);不会去调用Object上原型toString方法(返回对象的具体类型),所以采用obj.toString()不能得到其对象类型,只能将obj转换为字符串类型;因此,在想要得到对象的具体类型时,应该调用Object上原型toString方法。
本文用于学习和分享。欢迎指正。
参考资料:
ES6新增数据类型 Symbol和BigInt_敲起来blingbling的博客-优快云博客_symbol、bigint