在ES5的时候,我们认知的数据类型确实是 6种:Number、String、Boolean、undefined、object、Null。
ES6 中新增了一种 Symbol 。这种类型的对象永不相等,即始创建的时候传入相同的值,可以解决属性名冲突的问题,做为标记。
谷歌67版本中还出现了一种 bigInt。是指安全存储、操作大整数。(但是很多人不把这个做为一个类型)。
JS数据类型
Number、String、Boolean、Null、undefined、object、symbol、bigInt。
JS数据类型分类:
JS的基本类型和引用类型有哪些呢?
基本类型(基本类型): String、Number、boolean、null、undefined。
引用类型:object。里面包含的 function、Array、Date。
JS数据类型:
JS 中判断数据类型 typeof 输出分别是什么?
{ } 、[ ] 输出 object。
console.log( ) 输出 function。
注意一点:NaN 是 Number 中的一种,非Number 。
请注意这里 isNaN 被我转成 true 。
期间我一直在纠结 Number(‘as’) 输出 NaN ?NaN == NaN 为什么是 false。其实 js 规定的NaN 不等于NaN。
2、假设:Number('123') == NaN ?
肯定是false,Number('123’) 输出 123。
JS数据类型判断
1、typeof 操作符
// Numbers
typeof 37 === 'number';
typeof 3.14 === 'number';
typeof Math.LN2 === 'number';
typeof Infinity === 'number';
typeof NaN === 'number'; // 尽管NaN是"Not-A-Number"的缩写
typeof Number(1) === 'number'; // 但不要使用这种形式!
// Strings
typeof "" === 'string';
typeof "bla" === 'string';
typeof (typeof 1) === 'string'; // typeof总是返回一个字符串
typeof String("abc") === 'string'; // 但不要使用这种形式!
// Booleans
typeof true === 'boolean';
typeof false === 'boolean';
typeof Boolean(true) === 'boolean'; // 但不要使用这种形式!
// Undefined
typeof undefined === 'undefined';
typeof declaredButUndefinedVariable === 'undefined';
typeof undeclaredVariable === 'undefined';
//bigint
console.log(typeof 10n); // → bigint
console.log(typeof 10); // → number
// Objects
typeof {a:1} === 'object';
// Symbols
typeof Symbol() === 'symbol';
typeof Symbol('foo') === 'symbol';
typeof Symbol.iterator === 'symbol';
// 使用Array.isArray 或者 Object.prototype.toString.call
// 区分数组,普通对象
typeof [1, 2, 4] === 'object';
typeof new Date() === 'object';
// 下面的容易令人迷惑,不要使用!
typeof new Boolean(true) === 'object';
typeof new Number(1) === 'object';
typeof new String("abc") === 'object';
// 函数
typeof function(){} === 'function';
typeof class C{} === 'function'
typeof Math.sin === 'function';
typeof new Function() === 'function';
//null
typeof null === 'object'; // 从一开始出现JavaScript就是这样
JavaScript 最初的实现中,JavaScript 中的值是由一个表示类型的标签和实际数据值表示的。
对象的类型标签是 0。由于 null 代表的是空指针(大多数平台下值为 0x00),因此,null的类型标签也成为了 0,
typeof null就错误的返回了"object"
2、instanceof操作符
除了使用typeof来判断,还可以使用instanceof。instanceof运算符需要指定一个构造函数,或者说指定一个特定的类型,它用来判断这个构造函数的原型是否在给定对象的原型链上。
console.log(100 instanceof Number), //false
console.log('dsfsf' instanceof String), //false
console.log(false instanceof Boolean), //false
console.log(undefined instanceof Object), //false
console.log(null instanceof Object), //false
console.log([1,2,3] instanceof Array), //true
console.log({a:1,b:2,c:3} instanceof Object), //true
console.log(function(){console.log('aaa');} instanceof Function), //true
console.log(new Date() instanceof Date), //true
console.log(/^[a-zA-Z]{5,20}$/ instanceof RegExp),//true
console.log( new Error() instanceof Error),//true
基本数据类型中:Number,String,Boolean。字面量值不可以用instanceof检测,但是构造函数创建的值可以,如下:
var num = new Number(123);
var str = new String('dsfsf');
var boolean = new Boolean(false);
3 .constructor
constructor是prototype对象上的属性,指向构造函数。根据实例对象寻找属性的顺序,若实例对象上没有实例属性或方法时,就去原型链上寻找,因此,实例对象也是能使用constructor属性的。
var num = 123;
var str = 'abcdef';
var bool = true;
var arr = [1, 2, 3, 4];
var json = {name:'wenzi', age:25};
var func = function(){ console.log('this is function'); }
var und = undefined;
var nul = null;
var date = new Date();
var reg = /^[a-zA-Z]{5,20}$/;
var error= new Error();
function Person(){
}
var tom = new Person();
console.log(
tom.constructor==Person,
num.constructor==Number,
str.constructor==String,
bool.constructor==Boolean,
arr.constructor==Array,
json.constructor==Object,
func.constructor==Function,
date.constructor==Date,
reg.constructor==RegExp,
error.constructor==Error
);
//所有结果均为true
undefined和null没有constructor属性所以不可以可以通过constructor属性来判断类型。
4 Object.prototype.toString.call()
使用Object.prototype.toString.call()检测对象类型可以通过toString() 来获取每个对象的类型。
为了每个对象都能通过 Object.prototype.toString() 来检测,需要以 Function.prototype.call() 或者 Function.prototype.apply() 的形式来调用,传递要检查的对象作为第一个参数,称为thisArg。
var toString = Object.prototype.toString;
toString.call(123); //"[object Number]"
toString.call('abcdef'); //"[object String]"
toString.call(true); //"[object Boolean]"
toString.call([1, 2, 3, 4]); //"[object Array]"
toString.call({name:'wenzi', age:25}); //"[object Object]"
toString.call(function(){ console.log('this is function'); }); //"[object Function]"
toString.call(undefined); //"[object Undefined]"
toString.call(null); //"[object Null]"
toString.call(new Date()); //"[object Date]"
toString.call(/^[a-zA-Z]{5,20}$/); //"[object RegExp]"
toString.call(new Error()); //"[object Error]"