导航栏: 首页 评论列表

转载:[从jQuery看JavaScript]-数据类型和对象(Type and Object)(一)

默认分类 2011-04-25 18:18:02

转载:http://blog.csdn.net/natineprince/archive/2009/11/08/4787689.aspx

<

div>

<

div class="Apple-style-span" style="font-family:verdana, sans-serif;font-size:12px;line-height:18px;text-align:left;">

[从jQuery看JavaScript]-数据类型和对象(Type and Object)(一)

<

div class="blogstory" style="font-size:14px;line-height:21px;">

jQuery片段:
  1. var  
  2.    // Will speed up references to window, and allows munging its name. 
  3.     window =this
  4.    // Will speed up references to undefined, and allows munging its name. 
  5.     undefined, 
  6.    // Map over jQuery in case of overwrite 
  7.     _jQuery = window.jQuery, 
  8.    // Map over the $ in case of overwrite 
  9.     _$ = window.$, 
  10.  
  11.     jQuery = window.jQuery = window.$ =function( selector, context ) { 
  12.        // The jQuery object is actually just the init constructor 'enhanced' 
  13.        returnnewjQuery.fn.init( selector, context ); 
  14.     }, 
  15.  
  16.    // A simple way to check for HTML strings or ID strings 
  17.    // (both of which we optimize for) 
  18.     quickExpr = /^[^<]*(<(.|\s)+>)[^>]*$|^#([\w-]+)$/, 
  19.    // Is it a simple selector 
  20.     isSimple = /^.[^:#\[\.,]*$/; 
  在这一节,我们将讨论同一段jQuery片段的另一个知识点:数据类型和对象。为了让我们更好地理解代码,我们必须对这一部分内容深入了解。没有牢固的基础,是不可能构筑起坚实的堡垒的。

<

ul style="margin-top:5px;margin-bottom:5px;margin-left:1em;list-style-type:none;">

  • 内置数据类型

    内置数据类型,也称作固有数据类型,也就是JS的基本的数据类型。首先,让我们的大脑热一下身:回想一下,我们所有编程语言中实际可能运用到的数据都有些什么?
    基本如你所想,但实质上我们需要的只是有意义的文字而已。但对于电脑来说,它能认识的不是文字,而是逻辑电路中电平的高低。为此,我们在程序语言中,将这些高低电平转换成0和1,并使用这些二进制的数字串构造成人类更加好理解的数字逻辑。这些数字逻辑实际上就是所谓的数据类型了。(我承认我在胡说八道……)
    现在让我们看看JS是怎么理解这些数字逻辑,来让我们更好地使用它的(至少JS的设计者初衷是这样)。

    <

    ol style="margin-top:5px;margin-bottom:5px;margin-left:1em;list-style-type:none;">

  • undefined

    我们第一个看到的数据类型是undefined。或许很多人都会怀疑到——你没有搞错吧?undefined也是一种数据类型?!然后,我可以很镇定的告诉你,我没搞错……

    1. alert(undefined);// "undefined" 
    2. alert(typeofundefined);// "undefined" 
    3. alert(undefinedinstanceofObject);// "false" 
    4. alert(undefinedinstanceofundefined);// 语法错误:instanceof不能对undefined运算 
    5. alert(undefinedinstanceofUndefined);// 错误:"Undefined"未定义 


    看到上面的例子后,你还有疑问吗?
    联系一下我们前面所说的内容,如果调用一个没有声明的变量被直接调用,应该是会报错的,但上例没有,这就证明了undefined是JS里固有的变量。而且,根据typeof的结果,这个变量的数据类型是undefined。但它不是一个Object。而且不能用instanceof来判断他是不是undefined实例。
    那么,让我们来小结一下:undefined是一种类似单态的数据类型,他在全局作用域中以变量标识undefined存在,并且拥有唯一值undefined。
    除了上面所说的以外,还有一些需要注意的地方,请看下例:

    1. // 如何判断一个变量是否声明 
    2. alert(typeofx =="undefined");// true  
    3. alert(x==undefined);// 报错:"x"未定义 


    所以,如果你需要判断一个变量是否已声明,请使用typeof运算符,再和"undefined"这个字符串比较。如果你看得仔细,你或许会产生一个疑问:在上一节中不是说,如果在作用域链中找不到变量标识的时候,不是会创建空值标识吗?如果你有此一问,那么我得赞你一下,你真的用心思考了!这个问题将在稍后解答。

    1. varx; 
    2. alert(undefined =="undefined");// false 
    3. alert(x==undefined);// true 
    4. alert(x=="undefined");// false 


    另外,别以为undefined和字符串"undefined"是相等的,undefined不是字符串,而是另一种数据类型。

  • null

    相信看完undefined后,你已经不再怀疑null也是一种数据类型了吧。可是null又有何特别呢?请看:

    1. alert(null);// "null" 
    2. alert(typeofnull);// "object" 
    3. alert(nullinstanceofObject);// false 
    4. alert(nullinstanceofnull);// 报错:instanceof不能对null运算 


    你有感到奇怪了么?奇怪在哪里呢?嗯。我也觉得奇怪:为啥typeof的运算结果会是“object”,但instanceof判断又为false呢?但对于这个问题,能找到的唯一解答是如此描述的:
    “这点潜在的混淆是为了向下兼容。”
    但怎么向下兼容,我已经不能考究也不想考究下去了。我能告诉大家的是typeof null的返回值“object”是ECMA-262中规定的内容。并且这规定只是为了兼容性,实际上null并不是一个Object的实例。
    至此,我们再小结一下:null和undefined很相似,只是我们从根本上还是属于两种不同的数据类型。但是他们真的“不同”吗?让我们再来看看下面的例子:

    1. alert(null==undefined);// "true" 
    2. alert(null===undefined);// "false" 


    那么,看来JS解释器一般认为null和undefined是相等的了,虽然在严格等于运算时结果是false。(据说这是为了兼容以往的浏览器中没有undefined而设的。)
    由此引起了我们另一个思考:他们到底有何异同呢?实际上,导致他们严格等于运算返回false的是typeof的运算——在运算符节再详细解释吧。但他们其实还有一个语言规范层级的区别:undefined是作为Global对象的属性存在的,而null则是ECMA规范中设定的一个字面值。换句话说,undefined是存放在Global中的属性(所以像单态),而null是解释执行时产生的值。然而对他们的值作强制转换还是相同的,如:

    1. alert(!undefined);// "true" 
    2. alert(!!undefined);// "true" 
    3. alert(!null);// "true" 
    4. alert(!!null);// "true" 
    5. // 因此我们在使用if作判断的时候能直接判断变量是否为空或未定义 
    6. alert(parseInt(undefined));// "NaN" 
    7. alert(parseInt(null));// "NaN" 
    8. // 注意NaN连自己也是不相等 


    还值得注意的是,和其他语言不同,他们和0值是不相等的,除非转换成布尔值:

    1. alert(undefined==0);// "false" 
    2. alert(null==0);// "false" 
    3. alert(!undefined==!0);// "true" 
    4. alert(!null==!0);// "true" 


    另外,它们都不带属性的:

    1. alert(null.a);// "错误: null has no properties" 
    2. alert(undefined.a);// "错误: undefined has no properties" 


    最后,提一点我在项目中经常看到的,某些程序员喜欢将一些控件的属性是否定义的判定交给字符串"undefined"和null,但实际上只有null在起作用呢。

  • boolean

    下一个我们看到的类型是boolean,也就是我们常用来判断真假的布尔型。它的值只有两个:true和false。让我们来看看它的特点:

    <

    div class="dp-highlighter" style="margin:18px 0px;font-family:Consolas, 'Courier New', Courier, mono, serif;font-size:12px;background-color:rgb(231, 229, 220);padding-top:1px;">

    <

    ol start="100" class="dp-c" style="margin-bottom:1px;margin-left:1em;list-style-type:decimal;border-top-style:none;border-right-style:none;border-bottom-style:none;border-left-style:none;border-width:initial;border-color:initial;list-style-image:initial;background-color:rgb(255, 255, 255);color:rgb(92, 92, 92);">

  • varx=true
  • alert(x);// "true" 
  • alert(typeofx);// "boolean" 
  • alert(!x==0);// "true" 
  • alert(x==1);// "true" 
  • alert(x==2);// "false" 
  • alert(parseInt(x));<span class="comment" style="border-top-s


    >> 留言评论