类型判断
- 源码地址:
/src/shared/util.js
- 描述:用于判断、检查、输出参数的类型
源码
1. isPrimitive
JS 原始类型:
- Null, Undefiend, String, Number, Boolean, Object, Symbol
JS 引用类型:
- Object、Array、Function
null
当一个对象指向空了,此时可以赋值为 null
如果想要一个变量值为 Null , 只能在变量定义的时候让这个变量的值为 Null 才行。
undefined:
当一个变量声明了,但没有赋值,结果是 undefined
当函数没有明确返回值,却接收了,结果也是 undefined
如果一个变量是 undefined,然后和一个数字进行计算,那么结果是 NaN
NaN
NaN 是 Number 类型
NaN 属性是代表非数字值的特殊值,该属性用于指示某个值
1 |
|
2. _toString
1 |
|
3. toRawType
1 |
|
解析
[ toString ]
- JS 中所有对象都继承于 Object
1 |
|
- 因为所有对象都继承与 Object, 故所有对象都可以使用 Object 原型上的方法
根据上述内容,可得以下结论
每个给都有一个 toString() 方法,当该对象被表示为一个文本值时,或者一个对象以预期的字符串方式引用时自动调用。
默认情况下,toString() 方法被每个 Object 对象所继承。
若果此方法在自定义对象中未被覆盖,toString() 返回 “[object type]”,其中 type 是对象的类型。
1 |
|
- 覆盖默认的 Object.prototype.toString
1 |
|
[ toString 原理 ]
当 Object.prototype.toString 被调用时,会执行以下操作:
获取 this 对象的 [[Class]] 属性值
计算出三个字符串的连接字符串 –> “[object”, 第一步操作结果 Result(1), “]”
返回第二部的操作结果 Result(2)
[[Class]] 是一个内部属性,所有的对象(原生对象和宿主对象)都拥有该属性。
在规范中 [[Class]] 的定义
内部属性 | 描述 |
[[Class]] | 一个字符串值,表名了该对象的类型 |
[使用 toString 检测对象类型]
可以通过 toString()
来获取每个对象的类型。
为了每个对象都能通过 Object.prototype.toString()
来检测,需要以 Function.prototype.call()
或者 Function.prototype.apply()
的形式来调用,传递要检查的对象作为第一个参数,称为 thisArg
1 |
|
[ 语言标准 ]
ECMAScript 5.1 (ECMA-262)
15.2.4.2 Object.prototype.toString ( )
When the toString method is called, the following steps are taken:
If the this value is undefined, return “[object Undefined]”.
If the this value is null, return “[object Null]”.
Let O be the result of calling ToObject passing the this value as the argument.
Let class be the value of the [[Class]] internal property of O.
Return the String value that is the result of concatenating the three Strings “[object “, class, and “]”.
如 ECMAScript 5 和随后的 Errata 中所定义,
从 JavaScript 1.8.5 开始 toString() 调用 null 返回 [object Null], undefined 返回 [object Undefined]。
ES 6 调用 toString 可以检测 函数参数 arguments 返回 [object Arguments] , Error 返回 [object Error]