javascript权威指南 学习笔记之javascript数据类型

复制代码 代码如下:

  <!doctype html>

  <html>

  <head>

  <meta http-equiv="content-type" content="text/html; charset=UTF-8">

  <title>javascript数据类型</title>

  </head>

  <body>

  <script type="text/javascript">

  /**

  JavaScript中允许使用

  三种基本数据类型----数字,文本字符和布尔值。其中数字包括符点数.

  此外,它还支持两种小数据类型---null(空)和undefined(未定义),该两种小数据类型,它们各自只定义了一个值 。

  还支持复合数据类型---对象(object),注意数组也是一种对象

  另外,js还定义了一种特殊的对象---函数(function),注意:函数也是一种数据类型,真的很强大。。。

  除了函数和数组外,JavaScript语言的核心还定义的其他一些专用的对象。例如:Date,RegExp,Error......

  */

  /**

  三种基本数据类型

  */

  var $num = 111;

  var $str = "aaabbbccc";

  var $b = false;

  document.write("javascript中的各种数据类型:");

  document.write("<br/>$num的类型: "+typeof $num);//number

  document.write("<br/>$str的类型: "+typeof $str);//string

  document.write("<br/>$b的类型: "+typeof $b);//boolean

  /**

  两种小数据类型

  */

  var x ;

  document.write("<br/>x的数据类型:"+typeof x);//undefined

  var bbb = !x;//true

  document.write("<br/>bbb的数据类型:"+typeof bbb);//boolean

  document.write("<br/>两种小数据类型:"+typeof null+","+typeof undefined);//object,undefined

  /**

  特殊数据类型:函数

  */

  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

  /**

  对象数据类型,以下三种方式

  */

  //第一种方式:通过构造基本对象,为对象添加属性来达到

  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");

  //以下输出三种方式的结果

  document.write("<br/>obj的类型:"+typeof obj);//object

  document.write("<br/>ooo的类型:"+typeof ooo);//object

  document.write("<br/>oooo的类型:"+typeof oooo);//object

  /**

  数组 也是一种对象

  */

  var $array = [];

  var $arrayA = ["aaa","bbb",111,false];

  var $arrayB = new Array();

  document.write("<br/>$array的数据类型:"+typeof $array);//object

  document.write("<br/>$arrayA的数据类型:"+typeof $arrayA);//object

  document.write("<br/>$arrayB的数据类型:"+typeof $arrayB);//object

  </script>

  </body>

  </html>