javascript - 构造 (x = x || y,z) 中的逗号是什么意思?

标签 javascript syntax

所以我找到了答案,但这还不足以扩展我的知识库。

我一直在寻找什么 x = x || y, z 在 StackOverflow 中的意思

我找到了这个。 What does the construct x = x || y mean?

但问题是 , z 是做什么用的?

我经常看到这些表达 window.something = window.something || {}、jQuery

我已经知道,如果第一个参数返回 false,则 {} 将被分配给 something 属性。

我的问题是,, jQuery 有什么用?

有人可以启发我并告诉我这个非常重要的知识吗?

更新 2014 年 8 月 11 日

所以我尝试进行测试。

var w = 0, x = 1,y = 2,z = 3;
var foo = w || x || y, z; //I see that z is a declared variable
console.log(foo); //outputs 1

和这个一样。

var w = 0, x = 1,y = 2;
var z = function(){return console.log("this is z");}
var foo = w || x || y, z; //same as this
console.log(foo); //still outputs 1

另一个。

var w = 0, x = 1,y = 2;
var z = function(){return console.log("this is z");}
function foobar(){
this.bar = console.log(foo,z);
}(foo = w || x || y, z);
foobar(); //outputs 1 and string code of foobar

更改 (foo = w || x || y, z) 中 z 的值。

var w = 0, x = 1,y = 2;
var z = function(){return console.log("this is z");}
function foobar(){
this.bar = console.log(foo,z);
}(foo = w || x || y, z=4);
foobar(); //outputs 1 and 4

我假设在函数的 之后将变量放在 ( ) 中与声明新变量相同。

另一个测试。

var w = 0, x = 1,y = 2,z = 1;
function foobar(){
    var bar = 10,z=2;
        console.log(z);
}(foo = w || x || y, z=4);
console.log(foo,z); // Seems that foo is public and made an output
foobar(); // outputs the z = 2 inside and disregards the z = 4 from (..., z=4)
console.log(z); // It seems that z is 4 again after calling foobar

但是,在这样的场景中。 Link to JSFiddle

//Self-Executing Anonymous Function: Part 2 (Public & Private)
(function( skillet, $, undefined ) {
    //Private Property
    var isHot = true;

    //Public Property
    skillet.ingredient = "Bacon Strips";

    //Public Method
    skillet.fry = function() {
        var oliveOil;

        addItem( "\t\n Butter \n\t" );
        addItem( oliveOil );
        console.log( "Frying " + skillet.ingredient );
    };

    //Private Method
    function addItem( item ) {
        if ( item !== undefined ) {
            console.log( "Adding " + $.trim(item) );
        }
    }    
}( window.skillet = window.skillet || {}, jQuery ));

//Public Properties
console.log( skillet.ingredient ); //Bacon Strips

//Public Methods
skillet.fry(); //Adding Butter & Fraying Bacon Strips

//Adding a Public Property
skillet.quantity = "12";
console.log( skillet.quantity ); //12

//Adding New Functionality to the Skillet
(function( skillet, $, undefined ) {
    //Private Property
    var amountOfGrease = "1 Cup";

    //Public Method
    skillet.toString = function() {
        console.log( skillet.quantity + " " + 
                     skillet.ingredient + " & " + 
                     amountOfGrease + " of Grease" );
        console.log( isHot ? "Hot" : "Cold" );
    };    
}( window.skillet = window.skillet || {}, jQuery ));

try {
    //12 Bacon Strips & 1 Cup of Grease
    skillet.toString(); //Throws Exception
} catch( e ) {
    console.log( e.message ); //isHot is not defined
}

似乎如果您删除 , jQuery 它只会记录“Bacon Strips” 引用这个链接Link to another JSFiddle (,jQuery 已被移除)

我不太明白这个.. 但是为什么 ( ) 中的 , jQuery 在函数的 之后算作为已经包含jQuery库的代码完整运行的引用?

从代码中删除 $.trim 后,它似乎又可以正常工作了。但我仍然不明白这个引用是如何工作的。 Link to the JSFiddle without the , jQuery and $.trim

最佳答案

Comma Operator在 JavaScript 中计算操作数并返回最后一个(最右边)的值。通过 JS Operator Precedence , OR 运算将首先被评估,然后是赋值。

所以这个表达式 x = x || y, z 实际上是 (x = (x || y)), z。 OR 运算符将返回比较的 bool 结果,或者对于非 bool 类型,如果第一个操作数为真,则返回第一个操作数,否则返回第二个操作数。赋值运算符的优先级也高于逗号运算符,因此 x 将被赋值为 OR 返回的值。 z 的值不会对 OR 运算或赋值产生任何影响。事实上,它将在最后进行评估,这意味着它本质上是一个单独的语句,对该“表达式”中的任何其他内容都没有影响。我看不出以这种方式编写表达式有任何实际值(value)。

关于javascript - 构造 (x = x || y,z) 中的逗号是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25235471/

相关文章:

当浏览器选项卡未聚焦时,Javascript setInterval/setTimeout 不起作用

javascript onclick 撇号引号问题

bash - 带变量的括号扩展?

Java 类泛型表达式语法

bash - '$?' 在 bash 脚本中意味着什么?

javascript - VueJS 2 模板

javascript - 在环回生成的代码中放置远程 Hook (beforeRemote、afterRemote)代码的位置

c++ - ClassName& 作为返回类型是什么意思?

javascript - 如何突出显示我呈现的数据中的搜索词?

javascript - 如何组合这些 jQuery 语句?