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";
转载于:https://www.cnblogs.com/ranran/p/3748081.html
jQuery中的 ||相关推荐
- 深入jQuery中的data()
引入 data函数在jQuery中看起来很不起眼, 就像沙滩上一颗平凡的沙子, 但仔细一瞅, 却惊讶的发现data是jQuery中无比重要的一环, 甚至jQuery中各种事件都基于此. data有什么 ...
- jquery中如何以逗号分割字符串_百度知道
jquery中如何以逗号分割字符串_百度知道javascript本身就是带split方法的 定义和用法 split() 方法用于把一个字符串分割成字符串数组. 语法 stringObject.spli ...
- 关于JQuery中的ajax请求或者post请求的回调方法中的操作执行或者变量修改没反映的问题...
前段时间做一个项目,而项目中所有的请求都要用jquery 中的ajax请求或者post请求,但是开始处理一些简单操作还好,但是自己写了一些验证就出现问题了,比如表单提交的时候,要验证帐号的唯一性,所以 ...
- jQuery中$(function(){})与(function($){})(jQuery)的区别
首先,这两个函数都是在页面载入后执行的函数,其中两者的区别在于: 在jQuery中$(function(){})等同于jQuery(function(){}),另一个写法为jQuery(documen ...
- jQuery中常用的函数方法总结
jQuery中为我们提供了很多有用的方法和属性,自己总结的一些常用的函数,方法.个人认为在www.21kaiyun.com的紫微斗数星座在线排盘开发中会比较常用的,仅供大家学习和参考. 事件处理 re ...
- jQuery中的.height()、.innerHeight()和.outerHeight()
jQuery中的.height()..innerHeight()和.outerHeight()和W3C的盒模型相关的几个获取元素尺寸的方法.对应的宽度获取方法分别为.width()..innerWid ...
- JQuery中的事件以及动画
嘿嘿,今天学习了JQuery的事件以及动画,感觉即将学习完JQuery,在回忆起上周学习的JavaScript,感觉好多刚刚学习的知识点都记得模 糊啦,这个是很让失望的,这里只说明了一点,课是听过啦, ...
- jquery中输入验证中一个不错的效果
在表单的输入验证中,经常要当用户没能正确输入后,要提示"XXXX输入错误" 这一类的信息,如何能搞到动态一点呢,今天发现jquery中的一个不错的效果,笔记之. 1 包含jquer ...
- html 表单内容怎么获取不到,jquery中formdate一直获取不到对象中的[0]的值 包括本身也是一个空的数据怎么办?...
jquery中formdate一直获取不到对象中的[0]的值 包括本身也是一个空的数据怎么办? 再做一个前台的ajax方法 查网上用formdate方法上传.可是进了接口之后一直在控制台获取不到for ...
- jQuery中鲜为人知的的几个方法
转来学习一下 jQuery中鲜为人知的的几个方法 jQuery近些年来仍旧是web开发中最受欢迎的类库,虽然大家褒贬不一,但是仍旧不失为一款最流行的Javascript,在今天这篇文章中,我们将介绍几 ...
最新文章
- oracle学习 sql基本语法(三),Oracle数据库学习三
- eclipse导入项目Archive for required library cannot be read or is not a valid ZIP file
- ssh白名单_阿里云服务器ssh白名单
- 【Boost】boost库asio详解3——io_service作为work pool
- .net的retrofit--WebApiClient底层篇
- CentOs基础操作指令(运行级别)
- Linux下的压缩和解压缩命令——compress/uncompress
- Zebra 命令模式分析2
- 计算机网络Flash教学课件,flash动画教学课件
- c#量化交易_全市场股票量化交易
- 【Linux】文件及目录
- 软件工程和计算机科学考公务员,软件工程可以考公务员吗
- 面向对象,数据库交互的Spring-boot(新手)
- 计算机控制系统刘恩沧课后题答案,第八章2 提高计算机控制系统可靠性的技术措施_内蒙古农业大学:计算机控制技术_ppt_大学课件预览_高等教育资讯网...
- SQL 压力测试实战
- 大数据获取方法及常用工具学习
- IP 数据库,CSV 和 MDB 格式。
- 论文阅读-2022.1.7-Don’t be Contradicted with Anything CI-ToD: Towards Benchmarking Consistency for Task
- Tomcat的默认管理员密码是什么?
- 曲师大教务系统服务器,曲阜师范大学教务系统
热门文章
- wxpython bind自定义_wxpython 支持python语法高亮的自定义文本框控件的代码
- mysql和oracle数据库兼容性_oracle数据库兼容mysql的差异写法
- php比较两个数组的键名统计有交集的数量,php比较两个数组的键名并返回交集的函数array_intersect_key()...
- 联想面试智力题,听说前面三道题必考(有部份解答)
- vue图片image
- Scala学习笔记04:内建控制结构
- Java案例:数据表转换成XML文档
- 2017.9.12 人员雇佣 失败总结
- 2017.8.7 GT考试 思考记录
- 获取少女资源.html,战舰少女资源怎么得 资源来源汇总