逗号在构造中的含义是什么(x = x || y,z)?

Mik*_*nte 7 javascript syntax

所以我遇到了一个答案,但这还不足以扩展我的知识库.

我一直在寻找x = x || y, zStackOverflow中的意义

我找到了这个. 构造x = x ||是什么 你的意思是?

但问题是什么, z呢?

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

我已经知道如果false在第一个参数上返回,那么{}将被分配给该something属性.

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

有人可以启发我并用这个非常重要的知识吸引我吗?

更新 8/11/2014

所以我试着做试验.

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
Run Code Online (Sandbox Code Playgroud)

和它一样.

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
Run Code Online (Sandbox Code Playgroud)

另一个.

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
Run Code Online (Sandbox Code Playgroud)

改变z的值(foo = w || x || y, 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
Run Code Online (Sandbox Code Playgroud)

我假设在函数( )之后放置变量}与声明一个新变量相同.

另一个考验.

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
Run Code Online (Sandbox Code Playgroud)

但是,在这种情况下.链接到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
}
Run Code Online (Sandbox Code Playgroud)

似乎如果你删除, jQuery它只记录"培根条"请参阅此链接链接到另一个JSFiddle(, jQuery已删除)

我真的没有得到这个..但是为什么在jQuery库已经被包含的情况下,函数的, jQuery内部计算为代码完全运行的参考?( )}

具有$.trim从代码中删除,这似乎再次正常工作.但我仍然不知道这种引用是如何工作的. 没有jQuery和$ .trim链接到JSFiddle

nbr*_*oks 6

JavaScript中的逗号运算符计算操作数并返回最后一个(最右侧)的值.通过JS 运算符优先级,将首先评估OR运算,然后进行赋值.

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