使用 Object.prototype.toString.call() 判断参数类型

要了解 Object.prototype.toString.call(),就需要先知道 Object.prototype.toString 函数的作用。

Object.prototype.toString 执行过程

规范 ECMAScript 5.1 (ECMA-262) 中对Object.prototype.toString()的执行步骤描述如下:

When the toString method is called, the following steps are taken:

  1. If the this value is undefined, return "[object Undefined]".
  2. If the this value is null, return "[object Null]".
  3. Let O be the result of calling ToObject passing the this value as the argument.
  4. Let class be the value of the [[Class]] internal property of O.
  5. Return the String value that is the result of concatenating the three Strings "[object ", class, and "]".

简单翻译下。
当调用 toString 方法时,将执行以下步骤:
1、如果 this 值为 undefined ,返回 [object Undefined]
2、如果 this 值为 null,返回 [object Null]
3、将 this 值作为 ToObject 参数,ToObject 返回值赋给为 O
4、设置 class 的值为 O 对象的 [[Class]] 值;
5、返回 "[object "class"]" 的拼接结果。

注:ToObject 操作可将 Boolean、Number、String、Object 转为一个对象。

关于 [[class]] 的值,规范中有这么一段:

The value of the [[Class]] internal property is defined by this specification for every kind of built-in object. The value of the [[Class]] internal property of a host object may be any String value except one of "Arguments", "Array", "Boolean", "Date", "Error", "Function", "JSON", "Math", "Number", "Object", "RegExp", and "String". The value of a [[Class]] internal property is used internally to distinguish different kinds of objects. Note that this specification does not provide any means for a program to access that value except through Object.prototype.toString。

这里的重点是:[[class]] 的值有 "Arguments", "Array", "Boolean", "Date", "Error", "Function", "JSON", "Math", "Number", "Object", "RegExp", and "String"

了解了 Object.prototype.toString() 函数的执行过程,现在可以来看它有什么作用了。

Object.prototype.toString 作用

Object.prototype.toString 主要根据 this 值类型来返回不同的字符串。我们可以据此来判断 value 类型。

使用 Object.prototype.toString.call 判断类型

call 可改变上下文,也就是改变 this 值。我们将需要判断类型的值作为参数传进 Object.prototype.toString.call 里。

基本上所有类型除了 Object 都继承自 Object ,所以都自带 toString 方法,但有些类型重写了 toString 函数,比如

[1, 2].toString(); // "1, 2"

调用这些被重写的 toString 会影响结果,所以我们使用 Object.prototype.toString 来判断类型。当然,Object.prototype.toString 也可以被改写,但几乎不会有人那么做(毕竟改写 Object 方法会影响到其他所有对象),是相对安全的。

判断结果如下:

Object.prototype.toString.call(123);                     // "[object Number]"
Object.prototype.toString.call('123');                   // "[object String]"
Object.prototype.toString.call(true);                    // "[object Boolean]"
Object.prototype.toString.call({});                      // "[object Object]"
Object.prototype.toString.call(function a() {});         // "[object Function]"
Object.prototype.toString.call(null);                    // "[object Null]"
Object.prototype.toString.call(undefined);               //  "[object Undefined]"
Object.prototype.toString.call();                        //  "[object Undefined]"
Object.prototype.toString.call([1, 2]);                  // "[object Array]"
Object.prototype.toString.call(new Date());              // "[object Date]"

我们可以据此写一个函数用于判断类型。

function getType (value) {
  const str = Object.prototype.toString.call(value)
  return /^\[object (.*)\]$/.exec(str)[1]
}

ECMAScript 新规范中对于 Object.prototype.toString() 的执行步骤有所改变,这里不再叙述。有兴趣可自行查阅。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,452评论 0 10
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,009评论 19 139
  • You Don't Know JS: this & Object Prototypes Chapter 5: Pr...
    大橙子CZ阅读 612评论 0 2
  • 玻璃易碎,碎后修复的,裂痕依旧在。 情感易破,破后修复的,心却远了。 每一个不愿提起的故事后面都饱含主人公的情思。...
    二零一久阅读 316评论 3 1
  • 环形布局,可拖动,独立item设置,可设置中心view更自然,更自由 效果图 用法 引入 方法 1.可以直接在布局...
    坑吭吭阅读 1,667评论 0 1