zoukankan      html  css  js  c++  java
  • typeof instanceof


    typeof用以获取一个变量的类型,typeof一般只能返回如下几个结果:number,boolean,string,function,object,undefined
    instanceof用于判断一个变量是否某个对象的实例

    Use instanceof for custom types:

    1 var ClassFirst = function () {};
    2 var ClassSecond = function () {};
    3 var instance = new ClassFirst();
    4 typeof instance; // object
    5 typeof instance == 'ClassFirst'; //obviously this one is false
    6 instance instanceof Object; //true
    7 instance instanceof ClassFirst; //true
    8 instance instanceof ClassSecond; //false 

    Use typeof for simple built in types:

     1 'example string' instanceof String; // false
     2 typeof 'example string' == 'string'; //true
     3 'example string' instanceof Object; //false
     4 typeof 'example string' == 'object'; //false
     5 true instanceof Boolean; // false
     6 typeof true == 'boolean'; //true
     7 true instanceof Object; // false
     8 99.99 instanceof Number; // false
     9 typeof 99.99 == 'number'; //true
    10 function() {} instanceof Function; //true
    11 typeof function() {}; //function

    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 tricki:

    typeof null; //object
  • 相关阅读:
    [LuoguP1445][LOJ#10202]樱花
    [APIO/CTSC2007]动物园
    [LOJ#10157]皇宫看守
    python3安装crypto出错,及解决方法
    Qt之QuaZip编译-使用教程
    CentOS-NAT模式下(DHCP)联网
    CentOS桌面环境中网卡启动失败
    Linux基础--基础命令
    Linux基础 --1
    python面试题----4
  • 原文地址:https://www.cnblogs.com/kite-Runner/p/3529907.html
Copyright © 2011-2022 走看看