Array.isArray()

用来检验是不是数组

        var a = [1,2,3]
console.log(typeof a); // object
console.log(Array.isArray(a)); // true

可以看出 typeof 并不能检验数组,虽然 Array.isArray() 可以检验数组,但是 IE8 都不兼容

        var a = [1,2,3]
alert(Object.prototype.toString.call(a)) // [object Array]

这个方法可以兼容IE8 以及以下的浏览器

typeof

        function foo(){}
console.log(typeof foo); // function
console.log(typeof null); // object
console.log(typeof undefined); // undefined
console.log(typeof 'a'); // string
console.log(typeof 1); // number

这里 null 是基本类型,不是一个对象,这个是 JS 的一个 bug。还有一个值得注意的地方:

console.log(undefined == null);  // true

因为在 JS 中 undefined 是派生自 null 的

instanceof

        function foo(){}
var a = new foo()
console.log(a instanceof foo); // true

instanceof 是根绝原型链来查找的,如果函数 foo 的显示原型对象在 a 对象的隐式原型链上,则返回 true,否则返回 false

绿色的就是对象 a 的原型链

JS isArray、typeof、instanceof的更多相关文章

  1. 原生JS:delete、in、typeof、instanceof、void详解

    delete.in.typeof.instanceof.void详解 本文参考MDN做的详细整理,方便大家参考[MDN](https://developer.mozilla.org/zh-CN/doc ...

  2. 浅谈JS中的typeof和instanceof的区别

    JS中的typeof和instanceof常用来判断一个变量是否为空,或者是什么类型. typeof typeof运算符返回一个用来表示表达式的数据类型的字符串. typeof一般返回以下几个字符串: ...

  3. boost--BOOST_AUTO、typeof、result_of

    1.BOOST_AUTO BOOST_AUTO的功能类似于auto和any,可以用来定义任意类型数据,且可以在编译时自动推导出表达式的类型.BOOST_AUTO属于boost中的typeof库,使用需 ...

  4. indexOf、instanceOf、typeOf、valueOf详解

    1.indexOf() 该方法用来返回某个指定的字符串值在字符串中首次出现的位置. 语法:indexOf(searchvalue,fromindex);两个参数,参数一表示查询的字符串值,参数二可选表 ...

  5. this 、typeof、false、parseInt()、this、arguments、Array和object判断

    typeof typeof (undefined) 不会报错 undefined object Number boolean function String 返回值为字符串类型 false .fals ...

  6. null、undefined、typeof、instanceof

    目录 概述 null undefined typeof instanceof 概述 JavaScript(ECMAScript标准)里共有5种基本类型: undefined, null, Boolea ...

  7. js中的typeof和instanceof和===

    typeof: 用于判断number/string/boolean/underfined类型/function 不能判断:null和object ,不能区分object和Array instanceo ...

  8. 挑战:万能的slash! 判断js中“/”是正则、除号、注释?

    很久以前在其它地方就探讨和关注过这个问题,但都没有满意的解答. 看了zjfeihu 的帖子: <前端代码加亮插件(html,jss,css),支持即时加亮,运行代码>,再次提出这个比较经典 ...

  9. (四)JavaScript之[break和continue]与[typeof、null、undefined]

    7].break和continue /** * JavaScript 的break和continue语句 * break 跳出switch()语句 * break 用于跳出循环 * continue ...

随机推荐

  1. DB Intro - MySQL and MongoDB

    mysql> CREATE TABLE tutorials_tbl( tutorial_id INT, tutorial_title VARCHAR(100), tutorial_author ...

  2. Kudu Tablet design

    不多说,直接上干货! http://blog.csdn.net/lookqlp/article/details/51416829

  3. IAR6.1的工程迁移到IAR6.5不能用的解决方法

    1.重命名过时的CMSIS头文件 "... \ CMSIS \ CM3 \ CoreSupport \ core_cm3.h  比如:core_cm3.h.old 2.启用CMSIS:项目- ...

  4. 百度网页分享js代码

    1.小图标 <div class="bdsharebuttonbox"> <a href="#" class="bds_qzone& ...

  5. VCL

    vcl常用配置 不缓存摸一个资源 在vcl_recv中 if (req.url ~ "private") { return (pass); } 动静分离 先定一个多个backend ...

  6. Win2D 官方文章系列翻译 - 调整控件分辨率

    本文为个人博客备份文章,原文地址: http://validvoid.net/win2d-choosing-control-resolution/ 本文旨在讲解如何配置 Win2D XAML 控件使用 ...

  7. 接口调试,HttpWebRequest和HttpWebResponse使用,接口回调处理

    public void queryIdCardSelects { string url=“jiekoudizhi.html”; string param="jiekoucanshu" ...

  8. Mincost

    The cost of taking a taxi in Hangzhou is not a constant for each kilometer you travel: the first 4 k ...

  9. 什么是TOPO学

    拓扑,一个跟门萨同样古怪的“科技Word”.其定义,对绝大多数读者而言,不一定需要理解,但无妨知道———拓扑学,数学的一门分科,研究几何图形在一对一的双方连续变换下不变的性质.不少门萨题,来自拓扑学, ...

  10. Android - 页面返回上一页面的三种方式

    今年刚刚跳槽到了新公司,也开始转型做Android,由此开始Android的学习历程. 最近在解很多UI的bug,在解bug过程中,总结了在UI的实现过程中,页面返回上一页面的几种实现方式. 一. 自 ...