Fem*_*ond 10 javascript short-circuiting
这条线parent && (this.parent.next = this);是什么意思?它只是看起来像坐在那里,什么也不做,不是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;
}
Run Code Online (Sandbox Code Playgroud)
它在我正在看的动画文件的多个地方.这是另一个例子..(!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;
Run Code Online (Sandbox Code Playgroud)
tym*_*eJV 16
这是简写
if (parent) {
this.parent.next = this
}
Run Code Online (Sandbox Code Playgroud)
如果parent为false则不弹出(this.parent.next = this),例如:
parent = false;
parent && alert("not run");
Run Code Online (Sandbox Code Playgroud)
短路评估:
逻辑表达式从左到右进行评估,被称为"短路"评估,
variable && (anything); // anything is evaluated if variable = true.
variable || (anything); // anything is evaluated if variable = false
Run Code Online (Sandbox Code Playgroud)
它可以用于变量的赋值:
var name = nametemp || "John Doe"; // assignment defaults if nametemp is false
var name = isValid(person) && person.getName(); //assignement if person is valid
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
500 次 |
| 最近记录: |