zoukankan      html  css  js  c++  java
  • typeof and instanceof

    typeof:返回一个变量的类型,一般返回 number,boolean,string,function,object,undefined。

    instanceof : 判断某个变量是否是某个对象的实例。

    function test(a,b){
    return a+b;
    }

    var a = new test();

    console.log( a instanceof test);
    console.log(typeof test=="function");
    console.log(typeof a=="object");
    console.log(typeof a=="function");

    结果是:true,true,true,false

     

    typeof

    typeof 運算子可按下列兩種方式來使用︰

    1. typeof operand
    2. typeof (operand)
    

    typeof 運算子可返回字串,這些字串指出未求值的運算元的類型。operand 是指字串、變數、關鍵字、物件,其類型可被 typeof 返回。括弧並非必要的。

    假設你定義了以下變數︰

    var myFun = new Function("5+2")
    var shape="round"
    var size=1
    var today=new Date()
    

    typeof 運算子對以下變數返回以下結果︰

    typeof myFun 返回的是 function
    typeof shape 返回的是 string
    typeof size 返回的是 number
    typeof today 返回的是 object
    typeof dontExist 返回的是 undefined
    

    對於 truenull 關鍵字而言,typeof 運算子返回以下結果︰

    typeof true 返回的是 boolean
    typeof null 返回的是 object
    

    對於數字或字串而言,typeof 運算子返回以下結果︰

    typeof 62 返回的是 number
    typeof 'Hello world' 返回的是 string
    

    對於屬性值而言,typeof 運算子返回屬性裡的值的類型︰

    typeof document.lastModified 返回的是 string
    typeof window.length 返回的是 number
    typeof Math.LN2 返回的是 number
    

    對於方法和函數而言,typeof 運算子返回以下結果︰

    typeof blur 返回的是 function
    typeof eval 返回的是 function
    typeof parseInt 返回的是 function
    typeof shape.split 返回的是 function
    

    對於預先定義的物件而言,typeof 運算子返回以下結果︰

    typeof Date 返回的是 function
    typeof Function 返回的是 function
    typeof Math 返回的是 function
    typeof Option 返回的是 function
    typeof String 返回的是 function
    
  • 相关阅读:
    面向对象的三个基本特征(讲解)
    GridView 72般绝技
    Asp.net 将数据库里的记录转换成json
    jquery json asp.net 将各种对象:list ..等转换成
    sql2000 分页存储过程
    .NET中DataSet转化Json工具类
    从攻击者痕迹看内网常见命令
    从攻击者角度看SetMpreference小结
    Java NIO 实现服务端和客户端的通信示例
    spark streaming 监听器执行顺序
  • 原文地址:https://www.cnblogs.com/coolicer/p/2025447.html
Copyright © 2011-2022 走看看