美文网首页我爱编程
三、JS 进阶--判断 JS 的数据类型

三、JS 进阶--判断 JS 的数据类型

作者: 焰火青春 | 来源:发表于2018-05-27 14:36 被阅读21次

    判断 JS 的数据类型

      判断 JS 数据类型,也就是我们说的类型检测,本文提供五种方法,分别是 typeof 运算符、instanceof 操作符、Object.prototype.toString 方法、constructor 属性、duck type

    1、typeof 运算符

    typeof 算是最常见的了,使用它会返回一个字符串,适合函数对象和基本类型(js中的基本类型:number、string、boolean、null、undefined、object[对象])的判断

    3、Object.prototype.toString.call( ) 方法

    var gettype = Object.prototype.toString;  
    gettype.call([]);        // object Array
    gettype.call(function(){});        // object Function
    gettype.call(null);        // object Null
    gettype.call('');        // object String
    gettype.call(undefined);        // object Undefined
    

    https://blog.csdn.net/yCharlee/article/details/52424603

    http://www.cnblogs.com/a546558309/p/3608194.html

    http://javascript.ruanyifeng.com/oop/prototype.html#

    相关文章

      网友评论

        本文标题:三、JS 进阶--判断 JS 的数据类型

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