zoukankan      html  css  js  c++  java
  • 【前端学习笔记01】JavaScript源生判断数据类型的方法

    原始类型(值类型):Undefined、Null、Number、String、Boolean;

    对象类型(引用类型):Object;

    typeof 

    可以识别标准类型,null外(返回Object);不能识别具体对象类型(Function除外)。

    用法举例:

    var num = 100;
    typeof num;  // 或 typeof(num) --> number 

    注:除number 、string、boolean、undefined、function类型外,其余类型都判断为object(包括null)。

    instanceof 

    可以判别内置对象类型、自定义对象类型;不能判别原始类型。

    var arr = [];
    arr instanceof Array; -->true

    Object.prototype.toString.call()

    可以识别标准类型、内置对象类型;不能识别自定义对象类型。

    Object.prototype.toString.call(123); // --> [object Number]

    constructor 

    可以判别内置对象类型、自定义对象类型、标准类型(但Underfined/Null不能识别)。

    var num = 100;
    num.constructor === Number; // --> true;

    返回构造器写法:

    function getConstructorName(obj){
        return (obj===undefined||obj===null)?obj:(obj.constructor&&obj.constructor.toString().match(/functions*([^(]*)/)[1]);
    } // match()把返回的 function Number() { [native code] } 中的Number拿到。
  • 相关阅读:
    VMware vSphere企业运维实战
    Unity 3D和2D手机游戏开发
    电商店铺装修推广教程
    uiiamgeview 设置圆角
    cgcolor regcolor tttatribute.
    谈待遇,
    不会自己 加的,也不会自己连线,
    代理,其他的类可以实现,而这个类不能实现,
    代理,
    TTTattribute 缺少 coretext
  • 原文地址:https://www.cnblogs.com/zachary93/p/6048047.html
Copyright © 2011-2022 走看看