首页 > 网站 > WEB开发 > 正文

从头开始学JavaScript (三)——数据类型

2024-04-27 14:14:14
字体:
来源:转载
供稿:网友

从头开始学javaScript (三)——数据类型

一、分类

基本数据类型:undefined、null、string、Boolean、number

复杂数据类型:object

object的属性以无序的名称和值对的形式 (name : value) 来定义

二、详解

1、undefined:undefined类型只有一个值:undefined,在使用var对变量进行声明但未初始化时,这个变量的值就是undefined.

包含undefined值的变量与尚未定义的变量是不一样的,以下这个例子可以说明:

    var demo1;//声明但未初始化    alert(demo1);//undefined    alert(demo2);//报错,demo2 is not defined

2、null:null类型只有一个值:null,从逻辑角度来看,null值表示一个空对象指针.

如果定义的变量准备在将来用于保存对象,那么最好将该变量初始化为null而不是其他值。这样一来,只要直接检测null值就可以知道相应的变量是否已经保存了一个对象的引用了,例如:

    if(car != null)    {        //对car对象执行某些操作    }

  实际上,undefined值是派生自null值的,因此ECMA-262规定对它们的相等性测试要返回true。

  alert(undefined == null); //true

  尽管null和undefined有这样的关系,但它们的用途完全不同。无论在什么情况下都没有必要把一个变量的值显式地设置为undefined,可是同样的规则对null却不适用。换句话说,只要意在保存对象的变量还没有真正保存对象,就应该明确地让该变量保存null值。这样做不仅可以体现null作为空对象指针的惯例,而且也有助于进一步区分null和undefined。

3、Boolean:Boolean类型有两个值:true和false,这两个值与数字值不是一回事,因此true不一定等于1,而false也不一定等于0。

需要注意的是:Boolean类型的字面值是区分大小写的,也就是说,True和False(以及其他形式的大小写混合)都不是Boolean值,只是标识符。

 虽然Boolean类型的字面值只有两个,但Javascript中所有类型的值都有与这两个Boolean值等价的值。要将一个值转换为其对应的Boolean值,可以调用类型转换函数Boolean(),例如:

    var message = 'Hello World';    var messageAsBoolean = Boolean(message);

  在这个例子中,字符串message被转换成了一个Boolean值,该值被保存在messageAsBoolean变量中。可以对任何数据类型的值调用Boolean()函数,而且总会返回一个Boolean值。至于返回的这个值是true还是false,取决于要转换值的数据类型及其实际值。下表给出了各种数据类型及其对象的转换规则。

QQggh5LMJHHUHCC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s1lmV5Ws7NzU1OTrrrk8nknTt3ZDOTySSrhMPhvb29O3fuWJYlNdLI9vb2wMDAysrKysrK9PQ0Cnfu3EEhmUxGIhG9V3dqEA6H5+bmpAPi55FIZG9vb2VlJZPJ7O3tTU9P40qFO3fuSI1+Fclkcnp6uoGbSI4l8Bm94DbY29tbWVmBqzSE+5C282fHyCV10tGSIhKJiOvDEff29sLhsMMXdehkHUU4HHY88VqSwrIsqAGxx6ZPYEJTeqRz7IVPZjIZOSkkzvr6un4I5Ig7+GYymenpaf3solfm5uYQRsXtt7e39Ui95xuCOQpOAFhK6QWdycnJlZWVPc1D6mdgYMC9MGtHf6412IkPHS0p3Oohk8kopbBq3N7e3t7ezmQyENErKyvb29u6U2ITu6SFycnJgYEB1DcxGsnxYXJy0rEk2qsRZebm5kR8hMNhLL8QphuSFLJK0/sg6zCwvr4eDocdlXt7ezipHn+REPY8NTrp6Izj7OgYFpFIFEvZcSxpR/CRhF7QgSZo7kH7zMTt5c/JZNIx/MmBdLSkSCaTSik9X+2QFJgYoDzm5ub0Fx8wQx5bKYURiNz1nTt3IpGIUor54bZGVkt6pYRLz3WVbuApKZD1BfLiIxKJoMYRB2tFNERht/7Qz+L5FkaanZycnP4riNeRSMRTADmuwuflIOlw4M+eu9rOn2tlEIkPHS0p9qqqQte/euoCGkI0rENS6IfIClUq+dVFW6O/tpA3vniVK2UEQVGT9UgKHf8XH/A6CZGZTAaJ38nJSfcKTMcR07e3t3Vpi4QKGtcXZ+6VpVJKPvuwL

上一篇:html实体转换

下一篇:node js co分析1

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表