換個(gè)角度看javascript--數(shù)據(jù)類(lèi)型

          之所以說(shuō)換個(gè)角度是指我現(xiàn)在對(duì)javascript的理解與我以往對(duì)javascript的理解。在這種理解的轉(zhuǎn)變中最大的轉(zhuǎn)變是對(duì)函數(shù)的理解,以及隨之而來(lái)的對(duì)javascript的對(duì)象,尤其是對(duì)象屬性的理解上的變化。簡(jiǎn)單的說(shuō),現(xiàn)在理解的函數(shù),不是和變量類(lèi)型同級(jí)的概念,而是變量類(lèi)型的一種,即函數(shù)也是一個(gè)對(duì)象,一個(gè)可以象數(shù)字,字符串一樣賦值給一個(gè)變量的實(shí)體。這個(gè)和C里將指針指向函數(shù)有些類(lèi)似,但我一直都是把javascript類(lèi)比java來(lái)理解。

          首先對(duì)javascript的類(lèi)型再熟悉一遍

          javascript中變量(variable)的值(value)有這樣幾種類(lèi)型(type):
          (ECMAscript Language Specification Edition 3 24-Mar-00)
          8.1 The Undefined Type
          The
          Undefined type has exactly one value, called undefined. Any variable
          that has not been assigned a value has the value undefined.
          8.2 The Null Type
          The Null type has exactly one value, called null.
          8.3 The Boolean Type
          The Boolean type represents a logical entity having two values, called true and false.
          8.4 The String Type
          The String type is the set of all finite ordered sequences of zero or more 16-bit unsigned integer values
          (“elements”).
          8.5 The Number Type
          The Number type has exactly 18437736874454810627 (that is, 264?253+3) values, representing the doubleprecision
          64-bit format IEEE 754 values as specified in the IEEE Standard for Binary Floating-Point Arithmetic,
          except that the 9007199254740990 (that is, 253?2) distinct “Not-a-Number” values of the IEEE Standard are
          represented in ECMAscript as a single special NaN value. (Note that the NaN value is produced by the program
          expression NaN, assuming that the globally defined variable NaN has not been altered by program execution.)
          8.6 The Object Type
          An Object is an unordered collection of properties. Each property consists of a name, a value and a set of attributes.

          我們比較關(guān)注面向?qū)ο螅詫?duì)其中的Object類(lèi)型多加留意。
          如上面的定義,Object類(lèi)型是無(wú)序的屬性的集合。每個(gè)屬性(property)有名稱(chēng),值和一些性質(zhì)(attributes)(如只讀,可枚舉,不可刪,內(nèi)部的)。

          舉幾個(gè)例子:
          var a;//a現(xiàn)在對(duì)應(yīng)一個(gè)值,該值的類(lèi)型是Undefined
          a = 1;//現(xiàn)在a對(duì)應(yīng)一個(gè)Number類(lèi)型的值
          a = true;//現(xiàn)在a的類(lèi)型變了,變成一個(gè)Boolean類(lèi)型的值。
          a = "x";//a現(xiàn)在對(duì)應(yīng)一個(gè)String類(lèi)型的值
          //
          (注意"x"和new String("x")并不相同)
          //
          說(shuō)到這里想到j(luò)avascript里兩個(gè)相等操作符==和===
          //==只針對(duì)基本的數(shù)據(jù)類(lèi)型,不對(duì)Object,如果待比較的兩個(gè)值都是Object的話(huà)
          //返回false,除非是同一個(gè)對(duì)象,如果有有一個(gè)是Object的話(huà),將之轉(zhuǎn)化為和另外一個(gè)值相同類(lèi)型的值
          //然后進(jìn)行比較。===則要求兩者類(lèi)型也相同。
          var b = new String("x");
          alert(a == b);//true
          alert(a===b);//false 因?yàn)轭?lèi)型不同
          alert(typeof a);//string
          alert(typeof b);//object
          a = {};
          //查看類(lèi)型的方法是typeof,可以寫(xiě)成typeof a也可以寫(xiě)成typeof(a)
          //參看typeof的說(shuō)明
          alert(typeof a);//object
          alert(typeof true);//boolean
          alert(typeof(typeof true))//string

          typeof的執(zhí)行邏輯

          Undefined"undefined"
          Null
          "object"
          Boolean"boolean"
          Number"number"
          String"string"
          Object (native and doesn’t implement [[Call]])"object"
          Object (native and implements [[Call]])"function"
          Object (host)
          Implementation-dependent

          從上面可以看到對(duì)于Object類(lèi)型,typeof根據(jù)值的情況返回object或function。
          (注:所謂
          implement [[Call]]我的理解就是指是否可以作為函數(shù)調(diào)用,native有兩種一種是內(nèi)置的如Array,Date等另一種是用戶(hù)自己定義的。除此之外就是host即javascript的宿主提供的一些對(duì)象。)

          posted on 2006-09-16 17:03 一農(nóng) 閱讀(588) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): javascript


          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          公告

          南京 java輔導(dǎo)班 約等于免費(fèi) 詳見(jiàn)yuqiaotech.com

          導(dǎo)航

          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          統(tǒng)計(jì)

          常用鏈接

          留言簿(10)

          隨筆檔案

          文章分類(lèi)

          文章檔案

          相冊(cè)

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 辰溪县| 二连浩特市| 凭祥市| 梅州市| 迁西县| 焉耆| 商丘市| 清新县| 佛冈县| 乳山市| 沙河市| 巩留县| 云安县| 婺源县| 怀安县| 固镇县| 山丹县| 兖州市| 手游| 新建县| 延川县| 南开区| 台州市| 沅江市| 循化| 新建县| 鹤庆县| 托克逊县| 全南县| 云和县| 泰安市| 荣昌县| 崇左市| 电白县| 宝兴县| 滨州市| 舟山市| 新宾| 崇左市| 太湖县| 个旧市|