javascript權(quán)威指南 學(xué)習(xí)筆記之javascript數(shù)據(jù)類型
更新時間:2011年09月24日 19:16:57 作者:
JavaScript中允許使用三種基本數(shù)據(jù)類型 數(shù)字,文本字符和布爾值。其中數(shù)字包括符點數(shù).此外,它還支持兩種小數(shù)據(jù)類型 -null(空)和undefined(未定義),該兩種小數(shù)據(jù)類型,它們各自只定義了一個值 。
復(fù)制代碼 代碼如下:
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>javascript數(shù)據(jù)類型</title>
</head>
<body>
<script type="text/javascript">
/**
JavaScript中允許使用
三種基本數(shù)據(jù)類型----數(shù)字,文本字符和布爾值。其中數(shù)字包括符點數(shù).
此外,它還支持兩種小數(shù)據(jù)類型---null(空)和undefined(未定義),該兩種小數(shù)據(jù)類型,它們各自只定義了一個值 。
還支持復(fù)合數(shù)據(jù)類型---對象(object),注意數(shù)組也是一種對象
另外,js還定義了一種特殊的對象---函數(shù)(function),注意:函數(shù)也是一種數(shù)據(jù)類型,真的很強大。。。
除了函數(shù)和數(shù)組外,JavaScript語言的核心還定義的其他一些專用的對象。例如:Date,RegExp,Error......
*/
/**
三種基本數(shù)據(jù)類型
*/
var $num = 111;
var $str = "aaabbbccc";
var $b = false;
document.write("javascript中的各種數(shù)據(jù)類型:");
document.write("<br/>$num的類型: "+typeof $num);//number
document.write("<br/>$str的類型: "+typeof $str);//string
document.write("<br/>$b的類型: "+typeof $b);//boolean
/**
兩種小數(shù)據(jù)類型
*/
var x ;
document.write("<br/>x的數(shù)據(jù)類型:"+typeof x);//undefined
var bbb = !x;//true
document.write("<br/>bbb的數(shù)據(jù)類型:"+typeof bbb);//boolean
document.write("<br/>兩種小數(shù)據(jù)類型:"+typeof null+","+typeof undefined);//object,undefined
/**
特殊數(shù)據(jù)類型:函數(shù)
*/
function myFun(x){//..............aaa處
return x*x;
}
var myFunFun = function(x){//..............bbb處
return x*x;
}
alert(myFun);//aaa處
alert(myFunFun);//bbb處
document.write("<br/>myFun,myFunFun的類型: "+typeof myFun+","+typeof myFunFun);//function,function
/**
對象數(shù)據(jù)類型,以下三種方式
*/
//第一種方式:通過構(gòu)造基本對象,為對象添加屬性來達到
var obj = new Object();
obj.name = "yangjiang";
obj.sex = "sex";
//第二種方式:利用對象直接量
var ooo = {};
ooo.name = "yangjiang";
ooo.sex = "sex";
//第三種方式:定義類型(有點像JAVA語言中的類):此種方式最常用
function People(name,sex){
this.name = name;
this.sex = sex;
}
var oooo = new People("yangjiang","sex");
//以下輸出三種方式的結(jié)果
document.write("<br/>obj的類型:"+typeof obj);//object
document.write("<br/>ooo的類型:"+typeof ooo);//object
document.write("<br/>oooo的類型:"+typeof oooo);//object
/**
數(shù)組 也是一種對象
*/
var $array = [];
var $arrayA = ["aaa","bbb",111,false];
var $arrayB = new Array();
document.write("<br/>$array的數(shù)據(jù)類型:"+typeof $array);//object
document.write("<br/>$arrayA的數(shù)據(jù)類型:"+typeof $arrayA);//object
document.write("<br/>$arrayB的數(shù)據(jù)類型:"+typeof $arrayB);//object
</script>
</body>
</html>