What does the comma mean in the construction (x = x || y, z)?

So, I came across an answer, but this is not enough to expand my knowledge base.

I was looking for age, which means x = x || y, z x = x || y, z in stackoverflow

I found this. What is the construction x = x || y mean?

But the problem is, what is , z for?

I often see these expressions window.something = window.something || {}, jQuery window.something = window.something || {}, jQuery

I already know that if false was returned in the first argument, then {} will be assigned to the something property.

My question is: what is , jQuery for?

Can someone enlighten me and rob me of this very important knowledge?

UPDATE 11/11/2014

So, I tried to do the tests.

 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 

and this is the same as this.

 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 

other.

 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 

changing the value of z in (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 

I assume that placing variables inside ( ) after } function is the same as declaring a new variable.

Another test.

 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 

However, in such a scenario. JSFiddle Link

 //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 } 

It seems that if you delete , jQuery , it will only write Bacon Strips logs. See this link. Link to another JSFiddle ( , jQuery deleted)

I really do not understand. But why , jQuery inside the ( ) after } function is considered a link to fully run the code when the jQuery library is already on

Removing $.trim from the code seems to work fine again. But I still donโ€™t understand how this link works. JSFiddle reference without jQuery and $ .trim

+7
javascript syntax
source share
1 answer

JavaScript Comma Operator evaluates operands and returns the value of the latter (at most). According to JS Operator Priority , the OR operation will be executed first, and then the assignment.

So this expression is x = x || y, z x = x || y, z acts (x = (x || y)), z . The OR operator either returns a logical comparison result, or for non-boolean types, the first operand, if it is true, or the second operand otherwise. The assignment operator also takes precedence over the comma operator, so x will be assigned the value returned by OR. A value of z will not affect either the OR operation or the assignment. In fact, this will be evaluated last, which means that it is essentially a separate statement that does not affect anything else in this โ€œexpressionโ€. I see no practical value in writing an expression in this way.

+6
source share

All Articles