jquery源码片段如下:
// HANDLE: $(expr, $(...))
} else if ( !context || context.jquery ) {
return ( context || rootjQuery ).find( selector );
我认为context这里是一个jquery对象, rootjquery是$(document)
但他们返回的记过不应该是布尔值吗, 这里为什么是一个jquery对象.
所以||运算在这里做了什么? 他的作用是什么???
谢谢
过去多啦不再A梦2017-05-16 13:37:57
基础请看这里, 一目了然.
https://developer.mozilla.org...
多说一句, 在 ES2015 以前, JS 的函数是没有默认参数的.
为了像其他语言一样能用默认参数, 经常会这么写
function demo(arg){
arg = arg || true;
}
还有为了兼容不同浏览器 API 的时候, 也经常这么写.
if( !window.requestAnimationFrame ){
window.requestAnimationFrame = (window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (callback){
return setTimeout(callback, 1000 / 60);
});
}
滿天的星座2017-05-16 13:37:57
Javascript逻辑运算符可以在非Boolean的环境中使用.
尽管 && 和 || 运算符能够在非Boolean环境中使用, 但如果他们的返回值能够转换成Boolean值的话,也可以认为是Boolean运算
(来自逻辑运算符)
对于非Boolean的环境中:
当有一个false时,返回false一侧的值
当有两个false时,返回运算符之前(左侧)的值;
当有两个true时,返回运算符之后(右侧)的值。
(来源于JavaScript中的逻辑运算的返回值(逻辑与&&,逻辑或||,逻辑非!))