简体   繁体   English

jQuery:以下代码块中比较的含义是什么 - callback && function(){callback.call();}

[英]jQuery: What's the meaning of comparison in following code block - callback && function() {callback.call();}

Seeing this block from jQuery.scrollTo.js library (in v1.4 at line 184). jQuery.scrollTo.js库中查看此块(在第184行的v1.4中)。

function animate( callback ){
    $elem.animate( attr, duration, settings.easing, callback && function(){
        callback.call(this, target, settings);
    });
};

Curious to know how the comparison is going to work with 很想知道比较是如何工作的

callback && function() {callback.call(...)}; 

and what's exactly the meaning behind this. 这究竟是什么背后的含义。 Thanks in advance. 提前致谢。

The callback && function() {callback.call(...)}; callback && function() {callback.call(...)}; is doing two things: 正在做两件事:

  • testing whether a callback has been defined and 测试是否已定义callback
  • if it has, calling it in the correct context 如果有,请在正确的上下文中调用它

JavaScript has a feature called "short-circuiting logical expressions" . JavaScript具有称为“短路逻辑表达式”的功能 If a part of a logical expression cannot change the overall result, it is not going to be evaluated. 如果逻辑表达式的一部分无法更改整体结果,则不会对其进行求值。

If callback is undefined , then it evaluates to false in the context of a logical expression (it's "falsy" ). 如果callback undefined ,则它在逻辑表达式的上下文中计算为false(它是“falsy” )。 So the expression becomes the equivalent of false && ... , in which case it does not matter anymore what ... actually is, the result will always be false †1 . 因此,表达式变为等同于false && ... ,在这种情况下它不再重要...实际上,结果总是错误†1 JavaScript is not looking at the ... in this case. 在这种情况下,JavaScript并没有考虑...

If callback is not undefined (but a function), the first part of the logical expression evaluates to true (it's "truthy" ). 如果callback不是 undefined (但功能),逻辑表达式的第一部分的计算结果为真(它的“truthy”)。 At this point JavaScript evaluates the ... . 此时JavaScript会评估... The overall result happens to be a function expression. 总体结果恰好是函数表达式。 The function expression results in an anonymous function, which is then passed to jQuery's animate() . 函数表达式产生一个匿名函数,然后传递给jQuery的animate()

The callback.call() executes callback and determines the meaning of this within callback . 所述callback.call()执行callback ,并且确定的含义thiscallback So callback.call(this) makes sure that this refers to the same object as in the outer function. 所以callback.call(this)可以确保this指的是同一个对象作为外部函数。


†1 To be absolutely correct: The overall result of the expression will not be false , it will be undefined . †1 绝对正确:表达式的整体结果不会为false ,它将是undefined

callback && function() {callback.call(...)}; 

This will check if the callback is truthy (is not false, 0, "", null, undefined, NaN) and if it is truthy and second part of the statement will be evaluated which will return function that will be passed in as a argument. 这将检查回调是否真实(不是false,0,“”,null,undefined,NaN)并且如果它是真实的并且将评估语句的第二部分将返回将作为参数传递的函数。

Example: 例:

var test = true && function() {};
console.log(typeof test); // "function"

When used with Boolean operands, the && operator performs the Boolean AND operation on the two values: it returns TRue if and only if both its first operand and its second operand are true. 当与布尔操作数一起使用时,&&运算符对这两个值执行布尔AND运算:当且仅当其第一个操作数和第二个操作数都为真时,它才返回TRue。 If one or both of these operands is false, it returns false. 如果这些操作数中的一个或两个都为false,则返回false。

The actual behavior of this operator is somewhat more complicated. 该运算符的实际行为稍微复杂一些。 It starts by evaluating its first operand, the expression on its left. 它首先评估它的第一个操作数,即左边的表达式。 If the value of this expression can be converted to false (for example, if the left operand evaluates to null, 0, "", or undefined), the operator returns the value of the left-side expression. 如果此表达式的值可以转换为false(例如,如果左操作数的计算结果为null,0,“”或undefined),则运算符将返回左侧表达式的值。 Otherwise, it evaluates its second operand, the expression on its right, and returns the value of that expression. 否则,它将计算其第二个操作数,即右侧的表达式,并返回该表达式的值。

Therefore this expression callback && function(){ callback.call(this, target, settings); } 因此这个表达式callback && function(){ callback.call(this, target, settings); } callback && function(){ callback.call(this, target, settings); } returns second anonymous function. callback && function(){ callback.call(this, target, settings); }返回第二个匿名函数。 The benefit of this syntax is enabling to call callback function in context of plugin instance. 这种语法的好处是可以在插件实例的上下文中调用callback函数。

expr1 && expr2

The logical AND ( && ) operator returns expr1 if it can be converted to false; 如果逻辑AND( && )运算符可以转换为false,则返回expr1 ; otherwise, returns expr2 . 否则,返回expr2

Now in your example, a function object in callback will evaluate to true and the anonymous function is used; 现在在您的示例中, callback的函数对象将计算为true并使用匿名函数; if callback is undefined it will not create the anonymous function and pass undefined through. 如果callback未定义,则不会创建匿名函数并传递未定义的函数。

It's not a comparison. 这不是比较。 .animate() is a method so all of the items in brackets are arguments. .animate()是一种方法,因此括号中的所有项都是参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM