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

前端之家收集整理的这篇文章主要介绍了javascript – 逗号在构造中的含义是什么(x = x || y,z)?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我遇到了一个答案,但这还不足以扩展我的知识库.

我一直在寻找x = x ||的年龄y,z表示StackOverflow

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

但问题是,z是什么?

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

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

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

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

更新8/11/2014

所以我试着做试验.

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

和它一样.

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

另一个.

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

在(foo = w || x || y,z)中改变z的值.

  1. var w = 0,z=4);
  2. foobar(); //outputs 1 and 4

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

另一个考验.

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

但是,在这种情况下. Link to JSFiddle

  1. //Self-Executing Anonymous Function: Part 2 (Public & Private)
  2. (function( skillet,$,undefined ) {
  3. //Private Property
  4. var isHot = true;
  5.  
  6. //Public Property
  7. skillet.ingredient = "Bacon Strips";
  8.  
  9. //Public Method
  10. skillet.fry = function() {
  11. var oliveOil;
  12.  
  13. addItem( "\t\n Butter \n\t" );
  14. addItem( oliveOil );
  15. console.log( "Frying " + skillet.ingredient );
  16. };
  17.  
  18. //Private Method
  19. function addItem( item ) {
  20. if ( item !== undefined ) {
  21. console.log( "Adding " + $.trim(item) );
  22. }
  23. }
  24. }( window.skillet = window.skillet || {},jQuery ));
  25.  
  26. //Public Properties
  27. console.log( skillet.ingredient ); //Bacon Strips
  28.  
  29. //Public Methods
  30. skillet.fry(); //Adding Butter & Fraying Bacon Strips
  31.  
  32. //Adding a Public Property
  33. skillet.quantity = "12";
  34. console.log( skillet.quantity ); //12
  35.  
  36. //Adding New Functionality to the Skillet
  37. (function( skillet,undefined ) {
  38. //Private Property
  39. var amountOfGrease = "1 Cup";
  40.  
  41. //Public Method
  42. skillet.toString = function() {
  43. console.log( skillet.quantity + " " +
  44. skillet.ingredient + " & " +
  45. amountOfGrease + " of Grease" );
  46. console.log( isHot ? "Hot" : "Cold" );
  47. };
  48. }( window.skillet = window.skillet || {},jQuery ));
  49.  
  50. try {
  51. //12 Bacon Strips & 1 Cup of Grease
  52. skillet.toString(); //Throws Exception
  53. } catch( e ) {
  54. console.log( e.message ); //isHot is not defined
  55. }

似乎如果你删除它,jQuery它只记录“培根条”
请参阅此链接Link to another JSFiddle(,jQuery已删除)

我真的没有得到这个..但是为什么在一个函数之后的()里面的jQuery作为代码的参考,当jQuery库已经包含在内时,它会完全运行?

将$.trim从代码删除后,它似乎再次正常工作.但我仍然不知道这种引用是如何工作的.
Link to the JSFiddle without the,jQuery and $.trim

解决方法

JavaScript中的 Comma Operator评估操作数并返回最后一个(最右边)的值.通过JS Operator Precedence,将首先评估OR操作,然后进行分配.

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

猜你在找的JavaScript相关文章