1.1 JavaScript 数据类型
简单数据类型 (基本数据类型): Undefined ; Null ; Boolean ; Number ; String ; Symbol(ES6 新增)
复杂数据类型: Object,Object 本质上是由一组无序的名值对组成.
(引用类型除 Object 外, 还包括 Function ,Array,RegExp,Date 等等.)
1.2 判断 JavaScript 数据类型的方法
1.2.1 typeof
typeof-- 返回给定变量的数据类型, 可能返回如下字符串:
返回字符串 -- 数据类型
- 'undefined'--Undefined
- 'boolean'--Boolean
- 'string'--String
- 'number'--Number
- 'symbol'--Symbol
- 'object'--Object / Null (Null 为空对象的引用)
- 'function'--Function
typeof 返回的类型都是字符串形式, 需注意, 例如:
- alert(typeof 'abcd' == "string") => true
- alert(typeof 'abcd' == String) => false
typeof 对于 Function ,Array,RegExp,Date 类型统一返回'object'
- alert(typeof [] == 'object') => true
- alert(typeof new Date) => 'object'
typeof 是操作符而非函数, 所以可以使用圆括号, 也可以不使用.
- alert(typeof 'abcd'); / alert(typeof ('abcd'));
- 1.2.2 instanceof
语法:
object instanceof constructor
instanceof 运算符用来检测 constructor.prototype 是否存在于参数 object 的原型链上, 是则返回 true, 不是则返回 false.
- alert([1,2,3] instanceof Array) ---------------> true
- alert(new Date() instanceof Date)
- alert(function(){this.name="22";} instanceof Function) ------------> true
- alert(function(){this.name="22";} instanceof function) ------------> false
注: instanceof 后面一定要是对象类型, 且大小写不能错.
instanceof 只能用来判断两个对象是否属于实例关系, 而不能判断一个对象实例具体属于哪种类型.
1.2.3 constructor
constructor-- 返回对象对应的构造函数.
- alert({}.constructor === Object); => true
- alert([].constructor === Array); => true
- alert('abcde'.constructor === String); => true
- alert((1).constructor === Number); => true
- alert(true.constructor === Boolean); => true
- alert(false.constructor === Boolean); => true
- alert(function s(){}.constructor === Function); => true
- alert(new Date().constructor === Date); => true
- alert(new Array().constructor === Array); => true
- alert(new Error().constructor === Error); => true
- alert(document.constructor === htmlDocument); => true
- alert(window.constructor === Window); => true
Symbol 值通过 Symbol 函数生成, 是一个原始类型的值, 不是对象, 不能通过 constructor 判断
alert(Symbol().constructor); => undefined
注:
null 和 undefined 是无效的对象, 没有 constructor, 因此无法通过这种方式来判断.
函数的 constructor 不稳定.
当一个函数被定义时, JS 引擎会为其添加 prototype 原型, 然后在 prototype 上添加一个 constructor 属性, 并让其指向函数的引用.
但函数的 prototype 被重写后, 原有的 constructor 引用会丢失. 再次新建一个次函数的实例后, 其 constructor 指向的内容已发生改变.
因此为了规范开发, 在重写对象原型时, 一般都需要重新给 constructor 赋值, 以保证对象实例的类型不被更改.
1.2.4 Object.prototype.toString()
toString() 是 Object 的原型方法, 调用该方法, 默认返回当前对象的 [[Class]] . 这是一个内部属性, 其格式为 [object Xxx] , 是一个字符串, 其中 Xxx 就是对象的类型.
对于 Object 对象, 直接调用 toString() 就能返回 [object Object] . 而对于其他对象, 则需要通过 call / apply 来调用才能返回正确的类型信息.
- Object.prototype.toString.call(new Date);//[object Date]
- Object.prototype.toString.call(new String);//[object String]
- Object.prototype.toString.call(Math);//[object Math]
- Object.prototype.toString.call(undefined);//[object Undefined]
- Object.prototype.toString.call(null);//[object Null]
- Object.prototype.toString.call('') ; // [object String]
- Object.prototype.toString.call(123) ; // [object Number]
- Object.prototype.toString.call(true) ; // [object Boolean]
- Object.prototype.toString.call(Symbol()); //[object Symbol]
- Object.prototype.toString.call(new Function()) ; // [object Function]
- Object.prototype.toString.call(new Date()) ; // [object Date]
- Object.prototype.toString.call([]) ; // [object Array]
- Object.prototype.toString.call(new RegExp()) ; // [object RegExp]
- Object.prototype.toString.call(new Error()) ; // [object Error]
- Object.prototype.toString.call(document) ; // [object HTMLDocument]
- Object.prototype.toString.call(window) ; //[object global] window 是全局对象 global 的引用
参考: http://www.cnblogs.com/onepixel/p/8832776.html
来源: http://www.jianshu.com/p/424b30e81d9d