jQuery1.2.6 clean方法中有这么一段第一眼看去会让人晕掉的方法。完全不知其所言。 
“||, && 可以这样用?”,“这段东西最终返回的是个什么对象啊?” 
// Trim whitespace, otherwise indexOf won't work as expected 
var tags = jQuery.trim( elem ).toLowerCase(), div = context.createElement("div");

var wrap = 
// option or optgroup 
!tags.indexOf("<opt") && 
[ 1, "<select multiple='multiple'>", "</select>" ] ||

!tags.indexOf("<leg") && 
[ 1, "<fieldset>", "</fieldset>" ] ||

tags.match(/^<(thead|tbody|tfoot|colg|cap)/) && 
[ 1, "<table>", "</table>" ] ||

!tags.indexOf("<tr") && 
[ 2, "<table><tbody>", "</tbody></table>" ] ||

// <thead> matched above 
(!tags.indexOf("<td") || !tags.indexOf("<th")) && 
[ 3, "<table><tbody><tr>", "</tr></tbody></table>" ] ||

!tags.indexOf("<col") && 
[ 2, "<table><tbody></tbody><colgroup>", "</colgroup></table>" ] ||

// IE can't serialize <link> and <script> tags normally 
jQuery.browser.msie && 
[ 1, "div<div>", "</div>" ] ||

[ 0, "", "" ];

深入研究查询资料后才明白,这一段到低是想搞些干什么,也才被这巨牛的写法所折服。

// Logical AND && : the second operand will always be returned, no matter whatever it is, except the first operand is one of (0, -0, null, "", false, undefined, NaN) for such condition, the first operand will be returned.    
                
// Logical OR || : the first operand will always be returned, except the first operand is one of (0, -0, null, "", false, undefined, NaN). 
// in this situation the second operand will be returned, no matter what the second operand it is, even it's the same to the first one.

//<<Professional JavaScript for Web Developers 2nd Edition.pdf>> Page 52.

因为已经trim过了,前后都没有了空格,主要是前面没空字符串,所以此时判断是否以什么开头也就是startWith,最简单就是写成tags.indexOf("<opt"), 看法indexOf,返回值当startWith为true时,刚好返回的是0, 其它情况:  1,找到但是在字符串中间出现的返回值是大于0正数;2,完全没出现过时,返回为-1. 反正一样都是非0的数,而妙处就在在JavaScript定义中对number类对象,只是为0时,才被认为可转化为false,其它包括负数都被认为为true.

The indexOf() method returns the position of the first occurrence of a specified value in a string.

If the Boolean object has no initial value, or if the passed value is one of the following:

0, -0, null, "", false, undefined, NaN 
the object it is set to false. For any other value it is set to true (even with the string "false")!

This method returns -1 if the value to search for never occurs.

那么假设如果tag是以"<opt"开头的话,那indexOf的值就是0,而前面加一个!号后, !tag.indoexOf("<opt") 的值就为true了, 那么就相当于是 
true && [...], 再回头看看 && 的定义, 前一个值为false时才返回前一个参数值, 否则总是返回第二个参数(即便它自己也是false 或 NaN什么的)。

总之呢, jQuery原码中中这两个逻辑符号用的频率是相当相当的高,特别是 || 。 
jQuery中几乎没那几方法实现中不用它的。 用好它们的确可以使代码更简化,优雅,高效。

例如下段,其常被用于,有默认值的情况。a不行,让b上,b也不行时,那就只能c了。退而求其次的处理方式。 
// Start an animation from one number to another 
custom: function(from, to, unit){ 
this.startTime = now(); 
this.start = from; 
this.end = to; 
this.unit = unit || this.unit || "px";

jQuery中的&& ||的更多相关文章

  1. [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结

    本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...

  2. JQuery中的工具函数总结

    前提引入 前提当然也是要引入Jquery啦... <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js" typ ...

  3. JQuery中ajax的相关方法总结

    前提条件 话说是jquery中的ajax方法,那么前提条件当然是引入jquery啦. <script src="http://libs.baidu.com/jquery/1.9.0/j ...

  4. JavaScript jQuery 中定义数组与操作及jquery数组操作

    首先给大家介绍javascript jquery中定义数组与操作的相关知识,具体内容如下所示: 1.认识数组 数组就是某类数据的集合,数据类型可以是整型.字符串.甚至是对象Javascript不支持多 ...

  5. jQuery中的100个技巧

      1.当document文档就绪时执行JavaScript代码. 我们为什么使用jQuery库呢?原因之一就在于我们可以使jQuery代码在各种不同的浏览器和存在bug的浏览器上完美运行. < ...

  6. jquery中ajax用return来返回值无效

    jquery中,ajax返回值,有三种写法,只有其中一种是成功的 /** * async:false,同步调用 * 返回1:2 * 失败 * 分析:ajax内部是一个或多个定义的函数,ajax中ret ...

  7. JQuery中$.each 和$(selector).each()的区别详解

    PS:晚上在写页面时,发现了一个问题,$.each 和$(selector).each()有哪些区别?百度搜索关键词,首页显示出来一些前人的经验,总结一下,发上来. 1.$(selector).eac ...

  8. jQuery中Animate进阶用法(一)

    jQuery中animate的用法你了解多少呢?如果仅仅是简单的移动位置,显示隐藏,哦!天哪你在浪费资源!因为animate太强大了,你可以有很多意想不到的用法!让我们一起研究一下吧~~ 首先要了解j ...

  9. jquery 中jsonp的实现原理

    在同源策略下,在某个服务器下的页面是无法获取到该服务器以外的数据的,即一般的 ajax是不能进行跨域请求的.但 img.iframe .script等标签是个例外,这些标签可以通过 src属性请求到其 ...

  10. 解决上一篇jquery中on的疑惑

    内容都是来自:http://www.365mini.com/page/jquery-on.htm.这里做一下收藏.文章的最后  疑问和解答可以解决所有的疑惑  看了之后能更好的整篇文章. on()函数 ...

随机推荐

  1. 20141124-JS函数

    函数: 函数是由事件驱动或者当它被调用时执行的可重复色代码块. <head> <script> function hanshu() { alert("你好!" ...

  2. 转载:简单介绍Python中的try和finally和with方法

    用 Python 做一件很平常的事情: 打开文件, 逐行读入, 最后关掉文件; 进一步的需求是, 这也许是程序中一个可选的功能, 如果有任何问题, 比如文件无法打开, 或是读取出错, 那么在函数内需要 ...

  3. 向CodeBlocks的Project中添加calss文件时,出现No such file or directory错误的解决方案

    我们在CodeBlocks中编写程序时,一般要建立工程.现在建立工程first,然后建立类文件Person,并将其添加到first中, int main() { Person p; p.display ...

  4. 安装新版本的mysql数据库

    默认情况,在CentOS 6.8 下通过yum安装的是5.1.73版本,现在需求是安装5.7版本. -------------------------------------------------- ...

  5. sql server 中更改默认实例

    因为安装了多个版本的sql server,会造成同时存在多个实例的情况. 1.关闭旧版本的sql server实例,并设置为手动启动 2.按下图步骤操作,把tcp端口设置为1433 3.重启sql s ...

  6. [译]Redis大冒险

    原文:ALCA in Redis-land 一篇对使用Redis在NoSQL的世界中冒险之旅的总结. The legs of our journey 像每次出发一样,先对我们这次的旅程路线做个介绍: ...

  7. php文本操作方法集合比较第2页

    fgets和fputs.fread和fwrite.fscanf和fprintf 格式化读写函数fscanf和fprintf fscanf函数,fprintf函数与前面使用的scanf和printf 函 ...

  8. PHP流程控制语句

    流程控制语句分为两种(自己学到的就有俩不过在网上看还有两种) 1:条件控制语句即(if, if else , elseif , switch case) if语句不多说了,基本上大家都知道.if el ...

  9. Ztack学习笔记(1)-初识Ztack

    一.Zigbee协议 Zigbee是IEEE 802.15.4协议的代名词,是一种短距离.低功耗的无线通信技术.这一名称来源于蜜蜂的八字舞,因为蜜蜂(bee)是靠飞翔和“嗡嗡”(zig)地抖动翅膀的“ ...

  10. Microsoft Press Free eBook

    微软的免费的电子书, 都是Microsoft Press 出版的 有以下价格方面 Windows Server(大体上都是Windows Server 2012 ) Microsoft Azure(好 ...