zoukankan      html  css  js  c++  java
  • JavaScript中typeof正确使用

    首先要明确的是  在JS中使用typeof不能正确的判断是数组Array还是对象Object

    例:var arr = [ ] 

           typeof(arr)===“object”  //true

           结果返回的是true  但明显arr是一个数组

    正确判断数组的方法:

    (1)使用isarray方法

              var testArray = new array():

              testArray[0] = "testOne";

              testArray[1] = "testTwo";

              testArray[2] = "testThree";

              testArray[3] = "testFour";

              if(Array.isArray){

                     if(Array.isArray(testArray)){

                           document.write("这是一个数组");

                     }

    }

    (2)使用instanceof操作符

              var testArray = new array():

              testArray[0] = "testOne";

              testArray[1] = "testTwo";

              testArray[2] = "testThree";

              testArray[3] = "testFour";

              if(testArray instanceof Array){

                    document.write("这是一个数组");

    }

           

  • 相关阅读:
    HDU 4705 Y
    POJ 3614 Sunscreen
    Aizu 2170 Marked Ancestor
    POJ 3616 Milking Time
    POJ 2385 Apple Catching
    POJ 2229 Sunsets
    HDU 4678 Mine
    树的重量
    579Div3
    迷途之家2019联赛
  • 原文地址:https://www.cnblogs.com/zhuyan-dailycheck/p/9811522.html
Copyright © 2011-2022 走看看