胖在一方

出得厅堂入得厨房的胖子

导航

JavaScript(一) undefined

Posted on 2007-09-21 17:04  胖在一方  阅读(691)  评论(3)    收藏  举报
  ECMAScript变量可以存放两种类型的值,即原始值(primitive value)和引用值(reference value)
  ECMAScript有5种原始类型(primitive value)
  Undefined Null Boolean String Number
 
     ECMAScript 提供 typeof 运算符来判断一个值是否是在某种类型的范围内.
   可以用这个运算符判断一个值是否表示一种原始类型,如果它是原始类型,
   还可以判断是那种原始类型.
 
     typeof 运算符 typeof[()expression[]] ;
     返回一个用来表示表达式的数据类型的字符串。

    //    Undefined 类型只有一个值,即 undefined ,当声明的变量未初始化时,该变量的默认值是undefined
 var oTemp;
 alert(oTemp); // output  "undefined"
 alert(oTemp ==undefined) //true
 
 // 注意: 值undefined并不等同于未定义的值,但是typeof运算符并不真正区分这两种值.
 var tmp1;
 
 // var tmp2  //make sure this variable is not defined
 alert(typeof tmp1); // output "undefined"
 alert(typeof tmp2); // output "undefined" 即使tmp2是未声明的
 
 //如果对tmp2使用除 typeof 以外的运算符,会引发错误.(因为其他运算符只能运用在以声明的的变量上)
 //alert(tmp2==undifined) // causes error
 
 //如果函数为返回值时,返回值也是值 undefined
 
 function TestFunc()
 {
 }
 alert(TestFunc()==undefined); //output "true"  函数名后需要带() , 否则就和未声明的变量一样了 :P