jQuery中的&& ||
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中的&& ||的更多相关文章
- [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结
本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...
- JQuery中的工具函数总结
前提引入 前提当然也是要引入Jquery啦... <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js" typ ...
- JQuery中ajax的相关方法总结
前提条件 话说是jquery中的ajax方法,那么前提条件当然是引入jquery啦. <script src="http://libs.baidu.com/jquery/1.9.0/j ...
- JavaScript jQuery 中定义数组与操作及jquery数组操作
首先给大家介绍javascript jquery中定义数组与操作的相关知识,具体内容如下所示: 1.认识数组 数组就是某类数据的集合,数据类型可以是整型.字符串.甚至是对象Javascript不支持多 ...
- jQuery中的100个技巧
1.当document文档就绪时执行JavaScript代码. 我们为什么使用jQuery库呢?原因之一就在于我们可以使jQuery代码在各种不同的浏览器和存在bug的浏览器上完美运行. < ...
- jquery中ajax用return来返回值无效
jquery中,ajax返回值,有三种写法,只有其中一种是成功的 /** * async:false,同步调用 * 返回1:2 * 失败 * 分析:ajax内部是一个或多个定义的函数,ajax中ret ...
- JQuery中$.each 和$(selector).each()的区别详解
PS:晚上在写页面时,发现了一个问题,$.each 和$(selector).each()有哪些区别?百度搜索关键词,首页显示出来一些前人的经验,总结一下,发上来. 1.$(selector).eac ...
- jQuery中Animate进阶用法(一)
jQuery中animate的用法你了解多少呢?如果仅仅是简单的移动位置,显示隐藏,哦!天哪你在浪费资源!因为animate太强大了,你可以有很多意想不到的用法!让我们一起研究一下吧~~ 首先要了解j ...
- jquery 中jsonp的实现原理
在同源策略下,在某个服务器下的页面是无法获取到该服务器以外的数据的,即一般的 ajax是不能进行跨域请求的.但 img.iframe .script等标签是个例外,这些标签可以通过 src属性请求到其 ...
- 解决上一篇jquery中on的疑惑
内容都是来自:http://www.365mini.com/page/jquery-on.htm.这里做一下收藏.文章的最后 疑问和解答可以解决所有的疑惑 看了之后能更好的整篇文章. on()函数 ...
随机推荐
- [Bootstrap]组件(二)
按钮组 .btn-group>.btn : 一组.btn按钮包裹在.btn-group 外包元素.btn-group {position/display/} 按钮元素.btn <div ...
- java 自动装箱和自动拆箱
自动装箱 java执行Integer i = 100:编译器编译成Integer i = Integer.valueOf(100); Integer i = 100; //编译器编译成Integer ...
- Linux多线程编程(不限Linux)
前言 线程?为什么有了进程还需要线程呢,他们有什么区别?使用线程有什么优势呢?还有多线程编程的一些细节问题,如线程之间怎样同步.互斥,这些东西将在本文中介绍.我在某QQ群里见到这样一道面试题: 是否熟 ...
- opencv获取图片sift特征
利用opencv2.3来获取图片的sift特征,并输出到标准输出,可用重定向到文件. #include<cstdio> #include"opencv2/opencv.hpp&q ...
- sql中更新数据库用到declare @a in
declare @a in update TB_Class set @a=1,name='李小龙' where ID=1 这样就可以像更新哪个就更新哪个了 例如ibatisnet中需要更新的时候: & ...
- CSS3属性box-shadow使用教程
CSS3的box-shadow属性可以让我们轻松实现图层阴影效果.我们来实战详解一下这个属性. 1. box-shadow属性的浏览器兼容性先来看一个这个属性的浏览器兼容性: Opera: 不知道是从 ...
- Winform 导出Excel
private void 导出excelToolStripMenuItem_Click(object sender, EventArgs e) { ) { var saveFileDialog1 = ...
- C#中Delegate
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 11g RAC R2 体系结构---进程,日志
进程结构:Overview of Oracle Clusterware Platform-Specific Software Components When Oracle Clusterware is ...
- HTTP传递数据的几种方法
Http请求的时候,需要传递参数给后端,一般都是key-value的形式,传递的方法有很多种 例如需要传递的数据是 dict(key1=value1,key2=value2) 1. URL参数 把参数 ...