Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does expression && expression syntax mean? [duplicate]

What does this line parent && (this.parent.next = this); mean? It just looks like its sitting there, doing nothing, not an if statement or a promise or anything. Is there a name for this style of coding?

    var Particle = function(i, parent)
{
    this.next = null;
    this.parent = parent;
    parent && (this.parent.next = this);
    this.img = new Image();
    this.img.src = "http://www.dhteumeuleu.com/images/cloud_01.gif";
    this.speed = speed / this.radius;
}

Its in multiple places in this animation file I'm looking at. Here's another example.. (!touch && document.setCapture) && document.setCapture();

this.down = function(e, touch)
{
    e.preventDefault();
    var pointer = touch ? e.touches[0] : e;
    (!touch && document.setCapture) && document.setCapture();
    this.pointer.x = pointer.clientX;
    this.pointer.y = pointer.clientY;
    this.pointer.isDown = true;
like image 395
realisation Avatar asked Apr 15 '15 19:04

realisation


People also ask

What is expression meaning and example?

Word forms: plural expressions. 1. variable noun. The expression of ideas or feelings is the showing of them through words, actions, or artistic activities. Laughter is one of the most infectious expressions of emotion. [

What is expression in a sentence?

expression noun (SHOWING) the act of saying what you think or showing how you feel using words or actions: He wrote her a poem as an expression of his love. We've received a lot of expressions of support for our campaign.

What are expression used for?

The definition of an example of expression is a frequently used word or phrase or it is a way to convey your thoughts, feelings or emotions. An example of an expression is the phrase "a penny saved is a penny earned." An example of an expression is a smile.


2 Answers

It's shorthand for

if (parent) {
    this.parent.next = this
}
like image 77
tymeJV Avatar answered Oct 16 '22 16:10

tymeJV


if parent is false then not eject (this.parent.next = this), example:

parent = false;
parent && alert("not run");

Short-Circuit Evaluation:

As logical expressions are evaluated left to right, is named "short-circuit" evaluation,

variable && (anything); // anything is evaluated if variable = true.
variable || (anything); // anything is evaluated if variable = false

it's possible to use for assignment of variables:

var name = nametemp || "John Doe"; // assignment defaults if nametemp is false
var name = isValid(person) && person.getName(); //assignement if person is valid
like image 35
Jose Ricardo Bustos M. Avatar answered Oct 16 '22 16:10

Jose Ricardo Bustos M.