Javascript';s三元算子语义

Javascript';s三元算子语义,javascript,Javascript,我试图深入研究jQuery的src代码,在jQuery开发版本(3.2.1)的src代码的最开始部分,我们发现: ( function( global, factory ) { "use strict"; if ( typeof module === "object" && typeof module.exports === "object" ) { module.exports = global.document ? factory( global, t

我试图深入研究jQuery的src代码,在jQuery开发版本(3.2.1)的src代码的最开始部分,我们发现:

( function( global, factory ) {
"use strict";
if ( typeof module === "object" && typeof module.exports === "object" ) {
    module.exports = global.document ?
        factory( global, true ) :
        function( w ) {
            if ( !w.document ) {
                throw new Error( "jQuery requires a window with a document" );
            }
            return factory( w );
        };
} else {
    factory( global );
}

就我所知,我知道三元运算符在某些条件之后,但在这里它在赋值运算符
module.exports=global.document?
之后。你能解释一下吗?它在前面代码的上下文中做了什么?

三元运算符允许有条件地计算两个表达式(第一个条件为true,第二个条件为true)

赋值意味着,条件求值的结果被赋值给
模块.export

三元运算符是一种语法糖,其工作原理如下:

function ternary_operator(condition, expression1, expression2) {
    if (condition) {
        return expression1;
    } else {
        return expression2;
    }
}
因此,您可以将代码转换为:

module.exports = ternary_operator(global.document,
        factory( global, true ),
        function( w ) {
            if ( !w.document ) {
                throw new Error( "jQuery requires a window with a document" );
            }
            return factory( w );
        });
请注意:

factory( global, true )

它们都是表达式:激活
工厂
函数的结果是表达式;函数值的定义也是一个表达式

惰性评估 函数
三值_运算符
提供一个三值运算符。表达式在条件中求值,然后返回其中一个表达式。例如:

var l = ternary_operator(C, A, B);
1. evaluate C
2. evaluate A
3. evaluate B
4. activate function ternary_operator
5. assign to l the result of the function
Javascript三元运算符是惰性的:这意味着首先计算条件,然后只计算另一个表达式中的一个,从而获得性能

var l = C ? A : B;
1. evaluate C
2.1 if C is true evaluate A
2.2 if C is false evalutate B
3. assign to l the expression evaluated

三元运算符允许有条件地计算两个表达式(条件为真的情况下第一个表达式,否则第二个表达式)

赋值意味着,条件求值的结果被赋值给
模块.export

三元运算符是一种语法糖,其工作原理如下:

function ternary_operator(condition, expression1, expression2) {
    if (condition) {
        return expression1;
    } else {
        return expression2;
    }
}
因此,您可以将代码转换为:

module.exports = ternary_operator(global.document,
        factory( global, true ),
        function( w ) {
            if ( !w.document ) {
                throw new Error( "jQuery requires a window with a document" );
            }
            return factory( w );
        });
请注意:

factory( global, true )

它们都是表达式:激活
工厂
函数的结果是表达式;函数值的定义也是一个表达式

惰性评估 函数
三值_运算符
提供一个三值运算符。表达式在条件中求值,然后返回其中一个表达式。例如:

var l = ternary_operator(C, A, B);
1. evaluate C
2. evaluate A
3. evaluate B
4. activate function ternary_operator
5. assign to l the result of the function
Javascript三元运算符是惰性的:这意味着首先计算条件,然后只计算另一个表达式中的一个,从而获得性能

var l = C ? A : B;
1. evaluate C
2.1 if C is true evaluate A
2.2 if C is false evalutate B
3. assign to l the expression evaluated
在这种情况下,三元运算符不检查a=b,而是检查b。这是因为赋值运算符比三元运算符具有更高的运算符预衰减,这是有充分理由的

(a=b)?c:d;
在这种情况下,三元运算符实际上测试a=b。在这种情况下,它将b存储在a中并返回该值

在这种情况下,三元运算符不检查a=b,而是检查b。这是因为赋值运算符比三元运算符具有更高的运算符预衰减,这是有充分理由的

(a=b)?c:d;

在这种情况下,三元运算符实际上测试a=b。在这种情况下,它会将b存储在a中并返回该值。

三元运算符真正追求的是任何东西。如果它不是布尔值,它将被转换为布尔值。因此,如果global.document为null或未定义或“”或任何其他虚假值,则将被视为false,如果不为true,则表示如果global.document为true,则模块.exports将被分配为global.document。如果global.document为falsy,则函数(w)被分配给正在导出的module.exports。三元运算符的可能副本将真正用于任何操作。如果它不是布尔值,它将被转换为布尔值。因此,如果global.document为null或未定义或“”或任何其他虚假值,则将被视为false,如果不为true,则表示如果global.document为true,则模块.exports将被分配为global.document。如果global.document为falsy,则函数(w)被分配给正在导出的module.exports,该模块可能是