Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript idiom: !something && function()

Tags:

javascript

I have been looking at the source code of raphael.js and I see a lot of stuff like !variable && function() (e.g.: !svg.bottom && (svg.bottom = this); )

What does that exactly do? Does it check first and execute only if not true?

like image 930
cpf Avatar asked Apr 06 '10 17:04

cpf


People also ask

What is idiomatic JavaScript?

Idiomatic here means "How people who write JavaScript write JavaScript". It means "Natural to the native speaker". For example, returning an object is considered by some idiomatic JavaScript: function foo(){ return {x:3,y:5}; } var point = foo();

What is and $$ in JavaScript?

$ and $$ are valid variable names in JavaScript, they have no special meaning. Usually they set their value to library instances, in your example if you check the closure call, at the end of the file you'll see that $ is jQuery in this case if it is defined and $$ is cytoscape.

Why we use var self this?

When you have nested functions as in your example, this does not relate to the context of the outer function at all. Inner functions do share scope with the containing function, so developers will use variations of var that = this in order to preserve the this they need in the inner function. Save this answer.

What is self keyword in JavaScript?

The keyword self is used to refer to the current class itself within the scope of that class only whereas, $this is used to refer to the member variables and function for a particular instance of a class.


2 Answers

Correct. This is (ab)using short-circuit evaluation. A boolean expression is only executed as far as is needed to determine the result. In your example, if svg.bottom is non-null, then !svg.bottom is false, and the result of the && is false, so execution of the right hand side does not happen. It's basically equivalent to if(!svg.bottom)svg.bottom = this;

like image 88
Yuliy Avatar answered Sep 29 '22 21:09

Yuliy


When you have boolean operations, the compiler start check one by one each condition, and stops when its sure for the results - for example if you ask

if(a && b && c)
{    
}

if a is false, then the full boolean question is false, and compiler did not need to check b and c. This compiler feature is used to short the writing code for some cases.

This is (for me) a bad practice that writing code like.

!variable && function() 

instead of

if(!variable) function();

try to minimize the size of the javascript ?

Difficult to debug, and difficult to find what actually dose in many cases.

See this similar code.

unsigned uCycleCheckBox(unisgned uCur)
{
  return ((uCur <= 1) ? (uCur?0:1) : (uCur==4)?2:(uCur+1));
}

is the same think... hard to understand, hard to debug, hard to change and fix in case of problems.

For the comments on that, I suggest to read the books, Writing Solid Code, and Debugging the development process.

Writing solid code is more important than everything else.

like image 41
Aristos Avatar answered Sep 29 '22 22:09

Aristos