表达式和表达式语法是什么意思?(复制)

时间:2021-09-07 21:17:48

This question already has an answer here:

这个问题已经有了答案:

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?

这一行的父&和(this.parent)是什么?下一个=);的意思吗?它就像坐在那里,什么都不做,不是一个if语句,一个承诺或任何东西。这种类型的编码有名字吗?

    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();

它在这个动画文件中的多个位置。这是另一个例子。(!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;

2 个解决方案

#1


16  

It's shorthand for

这是缩写

if (parent) {
    this.parent.next = this
}

#2


7  

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

如果父类为false,则不弹出(this.parent)。下一个=),例如:

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

#1


16  

It's shorthand for

这是缩写

if (parent) {
    this.parent.next = this
}

#2


7  

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

如果父类为false,则不弹出(this.parent)。下一个=),例如:

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