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