JavaScript类型判断
整理一下js里面的类型判断
<script> let boolType = true; let numberType = 1; let stringType = 'abc'; let undefinedType = undefined; let nullType = null; let arrayType = [1,2,3,4]; let objectType = {name:'xiaoming',age:22}; let functionType = function(){console.log('hello')}; let symbolType = Symbol(); let nanType = NaN; /*** 操作符typeof 不能识别null,Array,都把它统一归为object类型 ***/ console.log('-----------------typeof-----begin-------------------') console.log('bool',typeof boolType); //boolean console.log('number',typeof numberType);//number console.log('string',typeof stringType);//string console.log('undefined',typeof undefinedType);//undefined console.log('null',typeof nullType);//object console.log('array',typeof arrayType);//object console.log('object',typeof objectType);//object console.log('function',typeof functionType);//function console.log('symbol',typeof symbolType); //symbol console.log('nan',typeof nanType); //number console.log('-----------------typeof-----end---------------------') /*** instanceof 通过原型链判断 不能识别出基本的数据类型 number、boolean、string、undefined、unll、Symbol ***/ console.log('-----------------instanceof-----begin-------------------') console.log('bool',boolType instanceof Boolean); console.log('number',numberType instanceof Number); console.log('string',stringType instanceof String); console.log('undefined',undefinedType instanceof Object); console.log('null',nullType instanceof Object); console.log('array',arrayType instanceof Array); console.log('object',objectType instanceof Object); console.log('function',functionType instanceof Function); console.log('symbol',symbolType instanceof Symbol); console.log('nan',nanType instanceof Number); console.log('-----------------instanceof-----end---------------------') /*** constructor 返回对创建此对象的函数的引用 但是可以人为的创建一个对象,更改它的原型 null、undefined没有construstor方法,因此constructor不能判断undefined和null ***/ console.log('-----------------constructor-----begin-------------------') console.log('bool',boolType.constructor === Boolean);// true console.log('number',numberType.constructor === Number);// true console.log('string',stringType.constructor === String);// true console.log('array',arrayType.constructor === Array);// true console.log('object',objectType.constructor === Object);// true console.log('function',functionType.constructor === Function);// true console.log('symbol',symbolType.constructor === Symbol);//true console.log('nan',symbolType.constructor === Number);//false console.log('-----------------constructor-----end---------------------') /*** 最推荐的方式 Object.prototype.toString.call() 运算符用来测试一个对象在其原型链中是否存在一个构造函数的 prototype 属性 ***/ console.log('-----------------Object.prototype.toString.call()-----begin-------------------') console.log('bool',Object.prototype.toString.call(boolType));//[object Boolean] console.log('number',Object.prototype.toString.call(numberType));//[object Number] console.log('string',Object.prototype.toString.call(stringType));//[object String] console.log('undefined',Object.prototype.toString.call(undefinedType));//[object Undefined] console.log('null',Object.prototype.toString.call(nullType));//[object Null] console.log('array',Object.prototype.toString.call(arrayType));//[object Array] console.log('object',Object.prototype.toString.call(objectType));//[object Object] console.log('function',Object.prototype.toString.call(functionType));//[object Function] console.log('symbol',Object.prototype.toString.call(symbolType)); //[object Symbol] console.log('nan',Object.prototype.toString.call(nanType)); //[object Number] console.log('-----------------Object.prototype.toString.call()-----end---------------------') /*** NaN是个比较特别的存在怎么判断是NaN ***/ console.log('nan',Object.prototype.toString.call(nanType) == '[object Number]' && nanType != nanType);// true