zoukankan      html  css  js  c++  java
  • JavaScript instanceof vs typeof

    Use instanceof for custom types
    var ClassFirst = function () {};
    var ClassSecond = function () {};
    var instance = new ClassFirst();
    typeof instance; // object
    typeof instance == 'ClassFirst'; //false
    instance instanceof Object; //true
    instance instanceof ClassFirst; //true
    instance instanceof ClassSecond; //false


    Use typeof for simple built in types
    'example string' instanceof String; // false
    typeof 'example string' == 'string'; //true

    'example string' instanceof Object; //false
    typeof 'example string' == 'object'; //false

    true instanceof Boolean; // false
    typeof true == 'boolean'; //true

    99.99 instanceof Number; // false
    typeof 99.99 == 'number'; //true

    function() {} instanceof Function; //true
    typeof function() {} == 'function'; //true


    Use instanceof for complex built in types
    /regularexpression/ instanceof RegExp; // true
    typeof /regularexpression/; //object

    [] instanceof Array; // true
    typeof []; //object

    {} instanceof Object; // true
    typeof {}; //object


    And the last one is a little bit tricky:
    typeof null; //object

  • 相关阅读:
    Noe4j启动警告
    SpringBoot
    MySQL数据库 java SQL语句区分大小写分析
    day24 模块的语法
    day23 re模块
    day22 常用模块02 序列化
    day21 常用模块01
    day20 面向对象06 MRO和C3算法
    day19 面向对象05 约束
    day18 面向对象04 反射
  • 原文地址:https://www.cnblogs.com/pugang/p/6257948.html
Copyright © 2011-2022 走看看