美文网首页
判断复合数据类型Object.prototype.toStrin

判断复合数据类型Object.prototype.toStrin

作者: zhouqs666 | 来源:发表于2016-06-21 22:16 被阅读83次

在 JavaScript 里使用 typeof 来判断数据类型,只能区分基本类型,即 “number”,”string”,”undefined”,”boolean”,”object” 五种。对于数组、函数、对象来说,其关系错综复杂,使用 typeof 都会统一返回 “object” 字符串。

要想区别对象、数组、函数单纯使用 typeof 是不行的。或者你会想到 instanceof 方法,例如下面这样:

var a = {};
var b = [];
var c = function () {};

//a b c 都是 Object 的实例
console.log(a instanceof Object) //true
console.log(b instanceof Object) //true
console.log(c instanceof Object) //true

//只有 Array 类型的 b 才是 Array 的实例
console.log(a instanceof Array) //false
console.log(b instanceof Array) //true
console.log(c instanceof Array) //false

//只有 Function 类型的 c 才是 Function 的实例
console.log(a instanceof Function) //false
console.log(b instanceof Function) //false
console.log(c instanceof Function) //true
从以上代码来看,要判断复合数据类型,可以如下判断:

//对象
(a instanceof Object) && !(a instanceof Function) && !(a instanceof Function)
//数组
(a instanceof Object) && (a instanceof Array)
//函数
(a instanceof Object) && (a instanceof Function)
更简便的方式,即是使用 Object.prototype.toString.call() 来确定类型,ECMA 5.1 中关于该方法的描述[1]是这样的:

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 "]“.
由于 JavaScript 中一切都是对象,任何都不例外,对所有值类型应用 Object.prototype.toString.call() 方法结果如下:

console.log(Object.prototype.toString.call(123)) //[object Number]
console.log(Object.prototype.toString.call('123')) //[object String]
console.log(Object.prototype.toString.call(undefined)) //[object Undefined]
console.log(Object.prototype.toString.call(true)) //[object Boolean]
console.log(Object.prototype.toString.call({})) //[object Object]
console.log(Object.prototype.toString.call([])) //[object Array]
console.log(Object.prototype.toString.call(function(){})) //[object Function]
所有类型都会得到不同的字符串,几乎完美。

相关文章

  • 判断复合数据类型Object.prototype.toStrin

    在 JavaScript 里使用 typeof 来判断数据类型,只能区分基本类型,即 “number”,”stri...

  • php 数据类型和数据类型转化

    一、数据类型 基础数据类型 复合数据类型 特殊数据类型 二、获取数据类型 三、打印数据类型和值 四、数据类型判断 ...

  • 基本类型和复杂类型

    基本类型:(简单数据类型) 复杂类型(复合数据类型) 如何判断数据的数据类型:typeof 关键字 语法 type...

  • JavaScript,判断复合数据类型

    概览 JavaScript中,判断复合数据类型大都是通过查找它的 原型链展开的。 内容 1 typeOf运算符2 ...

  • 如何实现 a == c, b == c成立,a == b 不成立

    在JavaScript中,利用 复合数据类型的拆箱,使两个不同的复合数据类型拆箱得到的值相同。 原理:复合数据类型...

  • Go复合数据类型

    复合数据类型:是以不同的方式组合基本类型可以构造出来的复合数据类型,在Golang中复合数据类型有数组、slice...

  • 学逻辑,练幽默课后第二次作业文章回答第4个问题

    4.判断有哪些分类?请举例说明 分类有:简单判断、复合判断,简单判断又分性质判断和关系判断,复合判断分联言判断、选...

  • 04 Golang数据类型

    分类 Go语言中的数据类型分为:基本数据类型和复合数据类型 基本数据类型 整型、浮点型、布尔型、字符串 复合数据类...

  • Go语言复合数据类型之数组

    在学习复合数据类型之前,我们得知道复合数据类型是由基本数据类型组合而成,和Java一样,Go语言也有基本数据类型,...

  • PHP数据类型

    数据类型 数据类型分为三种:标量型、复合型、特殊类型标量型: 复合型 特殊类型 数据类型检测 PHP中变量的数据类...

网友评论

      本文标题:判断复合数据类型Object.prototype.toStrin

      本文链接:https://www.haomeiwen.com/subject/qlbgdttx.html