隨筆 - 0, 文章 - 75, 評論 - 0, 引用 - 0
          數(shù)據(jù)加載中……

          從typeof()用法深入分析對象類型

          <script
          type="text/javascript">
          document.write("typeof()的用法<br>");
          document.write(typeof("")
          + "<br>"); //string
          document.write(typeof("hi") +
          "<br>");//string
          document.write(typeof(200) + "<br>");
          //number
          document.write(typeof(0) + "<br>");
          //number
          document.write(typeof(false) + "<br>");
          //boolean
          document.write(typeof(true) + "<br>");
          //boolean
          document.write(typeof(new Object()) + "<br>");
          //object
          document.write(typeof(new Array())+"<br>");
          //object
          document.write(typeof(new Date())+"<br>");
          //object
          document.write(typeof(new RegExp())+"<br>");
          //object
          document.write(typeof(null) + "<br>");
          //object
          document.write(typeof(new Function())+"<br>");
          //function
          document.write(typeof(undefined) + "<br>");
          //undefined
          </script>


          結(jié)果如下:

          typeof()的用法
          string
          string
          number
          number
          boolean
          boolean
          object
          object
          object
          object
          object
          function
          undefined

          由代碼結(jié)果分析得:
          typeof(null),typeof(Object),typeof(Array),typeof(Date),typeof(RegExp)結(jié)果都是object;
          typeof()方法只能分辨出number、string、function、boolean數(shù)據(jù)類型。
          那么如何能清楚分辨出其他Array、Date..類型呢?

          這里有一個有用的技巧就是運用Object.toString()方法來測試對象類型,其默認的返回形式如下的字符串:[object
          class],這里的class可以是用戶定義的任何對象,如Window,Object,Array等。我們只需要查看class的值就能辨別出對象的類型。

          好,看實驗吧!
          這里我創(chuàng)建了一個checkType函數(shù),用來檢測對象類型。
          <script language="javascript">
          var num =
          1221;
          var str = "abc123";
          var obj =
          {x:2,y:3};
          var arr = [3,"df",{}];

          var date = new Date();
          var func =
          function(){};
          var error = new
          Error();
          var exp = new RegExp();
          var
          n = null;
          var un;



          document.writeln(checkType(num));

          document.writeln(checkType(str));

          document.writeln(checkType(obj));

          document.writeln(checkType(arr));

          document.writeln(checkType(date));

          document.writeln(checkType(func));

          document.writeln(checkType(error));

          document.writeln(checkType(func));

          document.writeln(checkType(exp));

          document.writeln(checkType(n));

          document.writeln(checkType(un));


          function checkType(x){
            var typeAll =
          Object.prototype.toString.apply(x);
           
          return
          typeAll.substring(8,typeAll.length-1);

          }
          </script>

          結(jié)果:
          Number String Object Array Date Function
          Error Function RegExp Window Window

          posted on 2012-04-22 15:11 hantai 閱讀(3219) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 临高县| 望江县| 中卫市| 南澳县| 全州县| 土默特右旗| 福清市| 佳木斯市| 弥勒县| 隆化县| 万载县| 赫章县| 石嘴山市| 基隆市| 上饶县| 鱼台县| 镇远县| 洪江市| 天峨县| 九江市| 长宁区| 葵青区| 盐山县| 庄浪县| 苍溪县| 依安县| 红桥区| 普宁市| 台州市| 内丘县| 丹巴县| 昌乐县| 延川县| 集贤县| 项城市| 渭源县| 临泉县| 黄冈市| 砚山县| 竹北市| 沁水县|