这个布尔表达式如何计算?

发布于 2024-11-19 09:51:51 字数 956 浏览 4 评论 0原文

来自 Three.js:

this.sides.px && buildPlane( 'z', 'y',   1 * flip, - 1, depth, height, - width_half, this.materials[ 0 ] ); // px
this.sides.nx && buildPlane( 'z', 'y', - 1 * flip, - 1, depth, height, width_half, this.materials[ 1 ] );   // nx
this.sides.py && buildPlane( 'x', 'z',   1 * flip,   1, width, depth, height_half, this.materials[ 2 ] );   // py
this.sides.ny && buildPlane( 'x', 'z',   1 * flip, - 1, width, depth, - height_half, this.materials[ 3 ] ); // ny
this.sides.pz && buildPlane( 'x', 'y',   1 * flip, - 1, width, height, depth_half, this.materials[ 4 ] );   // pz
this.sides.nz && buildPlane( 'x', 'y', - 1 * flip, - 1, width, height, - depth_half, this.materials[ 5 ] ); // nz

什么这个布尔表达式返回吗?如果它返回一个布尔值,它去了哪里? (我没有看到作业!)它如何评估?

From Three.js:

this.sides.px && buildPlane( 'z', 'y',   1 * flip, - 1, depth, height, - width_half, this.materials[ 0 ] ); // px
this.sides.nx && buildPlane( 'z', 'y', - 1 * flip, - 1, depth, height, width_half, this.materials[ 1 ] );   // nx
this.sides.py && buildPlane( 'x', 'z',   1 * flip,   1, width, depth, height_half, this.materials[ 2 ] );   // py
this.sides.ny && buildPlane( 'x', 'z',   1 * flip, - 1, width, depth, - height_half, this.materials[ 3 ] ); // ny
this.sides.pz && buildPlane( 'x', 'y',   1 * flip, - 1, width, height, depth_half, this.materials[ 4 ] );   // pz
this.sides.nz && buildPlane( 'x', 'y', - 1 * flip, - 1, width, height, - depth_half, this.materials[ 5 ] ); // nz

What does this boolean expression return? If it returns a boolean value, where does it go?
(I see no assignment!) How does it evaluate?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦幻的味道 2024-11-26 09:51:51

你说得对,没有任务。这些语句利用了 && 运算符所使用的优化。如果右侧和左侧的计算结果都为 true,则 && 运算符的计算结果为 true。因此,如果左侧为假,它甚至不必检查右侧,因为无论如何结果都是假的,并且根本不对其进行评估。

因此,这段代码:

this.sides.px && buildPlane( 'z', 'y',   1 * flip, - 1, depth, height, - width_half, this.materials[ 0 ] ); // px

等价于这段代码:

if (this.sides.px) {
    buildPlane( 'z', 'y',   1 * flip, - 1, depth, height, - width_half, this.materials[ 0 ] );
}

You are right that there is no assignment. These statements utilize the optimization used by the && operator. The && operator evaluates to true if both the right hand and left hand sides evaluate to true. Thus, if the left hand side is false, it does not even have to check the right hand side, as the result will be false anyway, and it is not evaluated at all.

Thus, this code:

this.sides.px && buildPlane( 'z', 'y',   1 * flip, - 1, depth, height, - width_half, this.materials[ 0 ] ); // px

is equivalent to this code:

if (this.sides.px) {
    buildPlane( 'z', 'y',   1 * flip, - 1, depth, height, - width_half, this.materials[ 0 ] );
}
喜你已久 2024-11-26 09:51:51

如果第一部分为真,它仅评估表达式的第二部分。 &&是 JS 中的短路运算符:) 请参阅 http://en.wikipedia.org/wiki/短路_评估了解更多详细信息。

这些语句中的每一个都是单独评估的,但为了代码清晰起见,每个语句都可以替换为 if (this.sides.??) buildPlane(..); 。

It only evals the second part of the expression if the first part is true. && is a short-circuit operator in JS :) See http://en.wikipedia.org/wiki/Short-circuit_evaluation for more details.

Each of these statements are evaluated separately, but each could be replaced by if (this.sides.??) buildPlane(..); for code clarity.

赴月观长安 2024-11-26 09:51:51

实际上这个代码片段是 6 个语句,而不是单个表达式。每个语句都是一个表达式求值。

每个语句都是一个涉及 && 运算符的布尔表达式。为了计算这些表达式,JavaScript 首先计算左侧部分,然后仅当左侧部分为 true 时,才计算第二部分。第二部分是一个有副作用的函数调用,没有任何“返回”。

以这种方式使用 && 很常见。这是一种表达“仅在条件为真时才调用此函数”的方式。

Actually this code snippet is 6 statements, not a single expression. Each statement is an expression evaluation.

Each statement is a boolean expression involving the && operator. To evaluate these expressions, JavaScript first evaluates the left part, then ONLY if the left part is true, evaluates the second part. The second part is a function call that has a side effect, nothing is "returned."

The use of && in this fashion is common. It is a way to say "only make this function call if a condition is true."

嗫嚅 2024-11-26 09:51:51

这样的代码利用了短路,即逻辑与 (&&) 和逻辑或 (||) 只执行表达式的侧面(如果它们相关)。

假设我有 a && b.。如果 a 为 false,则 a &&无论 b 是什么,b 始终为 false,因此不需要检查。同样,如果我有一个|| b,如果 a 为 true,则 a ||无论b 是什么,b 始终为真,因此不需要检查。

因此Javascript只检查a。如果a恰好为真,那么它会检查b。如果 a 恰好是这样的形式(据我所知,可能是一个数字),那么他们会检查该数字是否 != 0。如果是,则执行第二个代码块。此操作的返回值将被丢弃,因为它没有分配到任何地方。

Code like that takes advantage of short circuiting, the fact that logical and (&&) and logical or (||) only execute the side of the expressions if they are relevant.

Say I have a && b. If a is false, a && b is always false no matter what b is so doesn't need to be checked. Similarly, if I have a || b, if a is true, then a || b is always true no matter what b is so doesn't need to be checked.

Therefore Javascript only checks a. If a happens to be true, then it checks b. If a happens to be in a form like that (probably a number from what I can tell), then they are checking if the number != 0. If yes, then it executes the second code block. The return value of this operation is discarded since it isn't assigned anywhere.

俏︾媚 2024-11-26 09:51:51

this.sides.px && buildPlane( 'z', 'y',   1 * flip, - 1, depth, height, - width_half, this.materials[ 0 ] );

将产生与此相同的结果:

if (this.sides.px) {
   buildPlane( 'z', 'y',   1 * flip, - 1, depth, height, - width_half, this.materials[ 0 ] );
}

它节省了几行代码,但与使用 if 语句相比,可能不是每个人都清楚。

This:

this.sides.px && buildPlane( 'z', 'y',   1 * flip, - 1, depth, height, - width_half, this.materials[ 0 ] );

will yield same result as this:

if (this.sides.px) {
   buildPlane( 'z', 'y',   1 * flip, - 1, depth, height, - width_half, this.materials[ 0 ] );
}

It saves a couple of lines of code, but may not be as clear to everyone versus using an if statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文