1.typeof xxx
注意:*typeof xxx为数组,null(空)和对象时返回的都是object,所以不能用来判断是否是数组!
*判断是否是数组的三种方法,1.instanceof 2.Object.prototype.toString.call()3.Array.isArray()
2.xxx instanceof 基本数据类型 返回布尔值
instanceof 运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上。
3.Object.prototype.toString.call(xxx) 用Object原型上的toString方法使用call来更改this
var arr = [1,2,2,3,5,4] var str = 'hello world' var str1 = new String('hello world') var flag = false var num = new Number(1) var num1 = 1 var obj = { a:1 } var n = null var udf = undefined // es6新数据类型 表示独一无二的值,最大的用法是用来定义对象的唯一属性名。 var syb = Symbol('kk') console.log(typeof arr) console.log(typeof str) console.log(typeof str1) console.log(typeof flag) console.log(typeof num) console.log(typeof num1) console.log(typeof obj) console.log(typeof n) console.log(typeof udf) console.log(typeof syb) console.log('--------------------') console.log(arr instanceof Array) console.log(arr instanceof Object) console.log('--------------------') console.log(num instanceof Number) console.log(num1 instanceof Number) console.log('--------------------') console.log(str instanceof String) console.log(str1 instanceof String) console.log('--------------------') console.log(syb instanceof Symbol) console.log('--------------------') console.log(Object.prototype.toString.call(arr)) console.log(Object.prototype.toString.call(str)) console.log(Object.prototype.toString.call(str1)) console.log(Object.prototype.toString.call(flag)) console.log(Object.prototype.toString.call(num)) console.log(Object.prototype.toString.call(num1)) console.log(Object.prototype.toString.call(obj)) console.log(Object.prototype.toString.call(n)) console.log(Object.prototype.toString.call(udf)) console.log(Object.prototype.toString.call(syb))