变量类型 |
构造函数 |
举例 |
类型检测typeof |
字符串 |
function String() |
var t = "chua"; var m = new String("chua"); |
"string" |
数字 |
function Number() |
var t = 12; var m = new Number(12); |
"number" |
布尔 |
function Boolean() |
var t = false; var m = new Boolean(0); var m1 = new Boolean(false); |
"boolean" |
数组 |
function Array() |
var t = [1,2,3]; var m = new Array(1,2,3); var m1 = new Array(3); m1[0] = 1; m1[1] = 2; m1[2] = 3; |
"object" |
对象 |
function Object() |
var t = {name:"chua"}; var m = new Object({name:"chua"}); var m1 = new Object(); m1.name = "chua"; |
"object" |
Null |
无,表示不该有值(无效值) ,null 被认为是对象的占位符 |
var t = null; //注意小写 function m(name){ if(name == null){alert(name)} } m(null); //null |
"object" |
Undefined |
无,表示变量未初始化值或函数中参数没有传递 |
var t = undefined;//小写 var tUn; alert(tUn);//undefined |
"undefined" |
函数 |
function Function() |
var t = function(){}; //构造了一个名为t的函数 |
"function" |
日期类型 |
function Date() |
var t = new Date();//获取当前时间 var m = new Date("January 1,2000 22:00:00"); //设置一个时间 |
"object" |
正则类型 |
Function RegExp(pattern, attributes) |
var t = /\d+/;//匹配一个或多个数字 var m = new RegExp("\\d+"); |
"object" |
Error类型 |
function Error() |
var t = new Error("语法错误"); alert(t.message); //"语法错误" |
"object" |