首页 > 其他 > 详细

数据类型(上)

时间:2016-08-20 19:25:03      阅读:203      评论:0      收藏:0      [点我收藏+]

ECMAScript中有5种简单数据类型:Undefined、Null、Boolean、   Number、和String。还有一个复杂数据类型--Object。

ECMAScript不支持任何创建自定义类型的机制,所以值都成为以上6中数据类型之一。

一.typeof操作符


  typeof操作符是用来检测变量 数据类型,对于值或者变量所以typeof
  操作符会返回如下字符串:

技术分享

         var box;
	 alert(typeof box);  //box是Undefined类型,值是undefined,类型返回的字符串是undefined

        var box=true;
	 alert(typeof box);  //box是Boolean类型,值是true,类型返回的字符串是boolean

	 var box=100;
	 alert(typeof box);  //box是Number类型,值是100,类型返回的字符串是number

	 var box=‘秦肯‘;
	 alert(typeof box);  //box是String类型,值是秦肯,类型返回的字符串是String

	 var box=null;
	 alert(typeof box);  //box是Null类型,值是null,类型返回的字符串是Object

         var box={};
	 alert(typeof box);  //box是Object类型,值是[object Object],类型返回的字符串是Object
      
	  var box=new Object; 
	 alert(box);  //box是Object类型,值是[object Object],类型返回的字符串是Object
      
	  function box(){

	  }
	 alert(typeof box);  //box是Function类型,值是function box(){},类型返回的字符串是function

  typeof 操作符可以操作变量,也可以操作字面量。虽然也可以这样使用:typeof(box),但是 typeof是操作符而非内置函数。


  ps:函数在ECMAScript中是对象,不是一种数据类型。所以,使用typeof来区分function和object是非常有必有的。

数据类型(上)

原文:http://www.cnblogs.com/linkhtml/p/5790916.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!