1. Object.prototype.toString.call()

每一个继承 Object 的对象都有 toString 方法,如果 toString 方法没有重写的话,会返回 [Object type],其中 type 为对象的类型。但当除了 Object 类型的对象外,其他类型直接使用 toString 方法时,会直接返回都是内容的字符串,所以我们需要使用call或者apply方法来改变toString方法的执行上下文。

const an = ['Hello','An'];
an.toString(); // "Hello,An"
Object.prototype.toString.call(an); // "[object Array]"

这种方法对于所有基本的数据类型都能进行判断,即使是 null 和 undefined 。

Object.prototype.toString.call('An') // "[object String]"
Object.prototype.toString.call(1) // "[object Number]"
Object.prototype.toString.call(Symbol(1)) // "[object Symbol]"
Object.prototype.toString.call(null) // "[object Null]"
Object.prototype.toString.call(undefined) // "[object Undefined]"
Object.prototype.toString.call(function(){}) // "[object Function]"
Object.prototype.toString.call({name: 'An'}) // "[object Object]"

Object.prototype.toString.call() 常用于判断浏览器内置对象。

2. instanceof

instanceof  的内部机制是通过判断对象的原型链中是不是能找到类型的 prototype。

使用 instanceof判断一个对象是否为数组,instanceof 会判断这个对象的原型链上是否会找到对应的 Array 的原型,找到返回 true,否则返回 false。

[]  instanceof Array; // true

但 instanceof 只能用来判断对象类型,原始类型不可以。并且所有对象类型 instanceof Object 都是 true。

[]  instanceof Object; // true

3. Array.isArray()

    • 功能:用来判断对象是否为数组

    • instanceof 与 isArray

      当检测Array实例时,Array.isArray 优于 instanceof ,因为 Array.isArray 可以检测出 iframes

      var iframe = document.createElement('iframe');
      document.body.appendChild(iframe);
      xArray = window.frames[window.frames.length-1].Array;
      var arr = new xArray(1,2,3); // [1,2,3] // Correctly checking for Array
      Array.isArray(arr);  // true
      Object.prototype.toString.call(arr); // true
      // Considered harmful, because doesn't work though iframes
      arr instanceof Array; // false
    • Array.isArray() 与 Object.prototype.toString.call()

      Array.isArray()是ES5新增的方法,当不存在 Array.isArray() ,可以用 Object.prototype.toString.call() 实现。

      if (!Array.isArray) {
      Array.isArray = function(arg) {
        return Object.prototype.toString.call(arg) === '[object Array]';
      };
      }

Object.prototype.toString.call() 、 instanceof 以及 Array.isArray()判断数组的方法的优缺点的更多相关文章

  1. typeof 、Object.prototype.toString和 instanceof

    数据类型 js 基本类型包括:Undefined  symbol null string boolean number js 引用类型包括:object array Date RegExp typeo ...

  2. Object.prototype.toString.call() 区分对象类型(判断对象类型)

    在 JavaScript 里使用 typeof 来判断数据类型,只能区分基本类型,即 “number”,”string”,”undefined”,”boolean”,”object” 五种.对于数组. ...

  3. Object.prototype.toString.call(arg)详解

    经常能碰到Object.prototype.toString.call对参数类型进行判断,一开始只知道怎么使用,却不了解具体实现的原理,最近恶补了一下相关知识,写个笔记加强理解,有什么不对的请指教. ...

  4. 前端面试题1:Object.prototype.toString.call() 、instanceof 以及 Array.isArray()三种方法判别数组的优劣和区别

    1. Object.prototype.toString.call() 每一个继承 Object 的对象都有 toString 方法,如果 toString 方法没有重写的话,会返回 [Object ...

  5. typeof操作符,返回数据类型Array.isArray()、Object.prototype.toString.call()

    源地址https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/typeof typeof操作符 // N ...

  6. instanceof, typeof, & Object.prototype.toString

    /** * * @authors Your Name (you@example.org) * @date 2016-11-18 09:31:23 * @version $Id$ */instanceo ...

  7. 类型判断----小白讲解typeof,instanceof,Object.prototype.toString.call()

    1.typeof只能判断基本类型数据, 例子: typeof 1 // "number" typeof '1' // "string" typeof true ...

  8. JS四种判断数据类型的方法:typeof、instanceof、constructor、Object.prototype.toString.call()

    1.typeof 1 console.log(typeof ""); //string 2 console.log(typeof 1); //number 3 console.lo ...

  9. JavaScript instanceof深度剖析以及Object.prototype.toString.call()使用

    本文由segementfalt上的一道instanceof题引出: var str = new String("hello world"); console.log(str ins ...

随机推荐

  1. Python3学习笔记----生成器

    生成器的作用 生成器可以在循环的过程中推算出后续的元素,不必创建完整的list,从而节省大量空间. 生成器的创建 1.把一个列表生成式的 [ ] 改写为( ) # -*- coding: UTF-8 ...

  2. PHP工程师必备知识整理

    一.http/https协议,tcp/ip协议,websocket,session,cookie 二.php:oop,thinkphp5,laravel 三.mysql.memcache.redis ...

  3. 关于 Expression is not assignable 错误

    1.在 Build Phases中导入  UIKit.framework 2.在pch中导入头文件 #import <UIKit/UIKit.h> 3.写一个分类 即可解决 贴出分类代码 ...

  4. TCGA一些数据库

    最出名,http://www.cbioportal.org/ 特色:最基本的简单分析基因突变.共表达/共突变的基因,下载数据也可以,最常看的应该还是oncoPrint那个. 详细用法:TCGA数据库的 ...

  5. xilinx Vivado的使用详细介绍(2):创建工程、添加文件、综合、实现、管脚约束、产生比特流文件、烧写程序、硬件验证

    xilinx Vivado的使用详细介绍(2):创建工程.添加文件.综合.实现.管脚约束.产生比特流文件.烧写程序.硬件验证 Author:zhangxianhe 新建工程 打开Vivado软件,直接 ...

  6. Crashing Robots(水题,模拟)

    1020: Crashing Robots 时间限制(普通/Java):1000MS/10000MS     内存限制:65536KByte 总提交: 207            测试通过:101 ...

  7. flex 垂直居中、两列对齐、自适应宽

    flex 垂直居中 <div id="parent"> <div id="child"> </div> </div&g ...

  8. cmd命令往MySQL数据库提交数据

    第一步:MySQL -V检查下载成功否第二步:mysql -u root -p 登陆密码第三步:创建一个数据库 create database if not exists 数据库name: 第四步:展 ...

  9. 论文选读二:Multi-Passage Machine Reading Comprehension with Cross-Passage Answer Verification

    论文选读二:Multi-Passage Machine Reading Comprehension with Cross-Passage Answer Verification 目前,阅读理解通常会给出 ...

  10. JAVA写接口傻瓜($)教程(三)

    接上一篇 补充:事实上java的类需要在src文件夹里写,classes文件夹只是会同步src文件夹里的类.如果在classes文件夹里操作java类,是会提示文件只读的.src文件夹中进行代码编写 ...