Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Why the comma expression?

 
Master Rancher
Posts: 1195
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran across some code in a NodeJS application recently that boils down to the following:

In A.js


In B.js:


Why is line 4 of the first file written like that?  If I understand correctly, the comma expression in the parens evaluates both operands and has a value equal to the second, so the value of the comma expression is the B.b function.  B.b is then called with a as an arg and the result is assigned back to a.  As you might expect, the output of the code is "a is now 3".  I see what it's doing, but what was the point of using (0, B.b) instead of just plain B.b?

The code was too nicely indented and commented for this to be obfuscation tactic.

Any thoughts?
 
Marshal
Posts: 67357
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot see any rhyme or reason to it.

If I had a dime for every time I saw weird things in code ...
 
Ryan McGuire
Master Rancher
Posts: 1195
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha!!!  It turns out I left out some code that I didn't realize had a bearing on the question.  Here's some new and improved code:

A.js:


B.js:


When B.b() is called from line 5 of A.js, the "this" inside of b() is "global" so this.console.log() is valid, and everything works fine.  

However, when B.b() is called as in line 6, "this" is the function b(), so this.console is undefined and the function this.console.log() is not found so a TypeError is thrown.

In the real code I found this in, the B.b() function never actually used "this" for anything, so that didn't trigger any alarms.  I guess A.js is using the (0, B.b) idiom to make sure B.b is executed in the global context in case it ever does use "this".

I apologize for not providing all the relevant code the first time around.

Disclaimer: I still don't quite get all the subtle details, but I at least know what to google do a web search for now.
 
Bear Bibeault
Marshal
Posts: 67357
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't do much Node programming so I don't know if this is a common idiom; but it's pretty opaque in any case. Surely there are clearer ways to so the same thing with being so arcane.
 
I am mighty! And this is a mighty small ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!