这篇文章主要讲解 js进行类型判断 Object.prototype.toString.call()
首先看一段ECMA中对Object.prototype.toString的解释:
Object.prototype.toString( )
When the toString method is called, the following steps are taken:
- Get the [[Class]] property of this object.
- Compute a string value by concatenating the three strings “[object “, Result (1), and “]”.
- Return Result (2)
1
2
3
4
5
6
7
8
9
10var oP = Object.prototype,
toString = oP.toString;
console.log(toString.call([123]));//[object Array]
console.log(toString.call('123'));//[object String]
console.log(toString.call({a: '123'}));//[object Object]
console.log(toString.call(/123/));//[object RegExp]
console.log(toString.call(123));//[object Number]
console.log(toString.call(undefined));//[object Undefined]
console.log(toString.call(null));//[object Null]
如果您觉得文章有用或对您有帮助,欢迎通过以下方式赞助我。 ♪(^∀^●)ノ
本文由 xxzkid | 我叫王也道长创作,采用
CC BY 3.0 CN协议 进行许可。
可自由转载、引用,但需署名作者且注明文章出处。
本文标题:js进行类型判断 Object.prototype.toString.call()
本文链接:https://xxzkid.github.io/2015/js-class/
本文标题:js进行类型判断 Object.prototype.toString.call()
本文链接:https://xxzkid.github.io/2015/js-class/