[英]property marked as undefined even though it is being used on the same line flawlessly?
in javascript, editing in Visual Studio Code, running on Google Chrome 在javascript中,在Visual Studio Code中进行编辑,并在Google Chrome上运行
if ((piece == null || piece.color !== us))
The above line runs property with no issues, however, when I change it to this: 上一行运行属性没有问题,但是,当我将其更改为此时:
if ((piece == null || piece.color !== us) && piece.color !== UNION)
or change this: 或更改此:
if (piece.color == null || piece.color == swap_color(us))
I get the following error: 我收到以下错误:
Uncaught TypeError: Cannot read property 'color' of undefined
Why do I get the error in the second case, yet the first case included that same property? 为什么在第二种情况下却出现错误,而第一种情况却包含相同的属性?
EDIT 编辑
I've been reading these answers on short circuiting, but it's not clicking, can someone help me form a boolean expression? 我一直在阅读有关短路的这些答案,但并不是单击,有人可以帮助我形成布尔表达式吗? Basically, piece.color could be one of three cases
基本上,piece.color可能是以下三种情况之一
I want to run 我要跑步
continue;
in situations when (piece.color == null || piece.color == swap_color(us))
and when piece.color !== UNION
在以下情况下:
(piece.color == null || piece.color == swap_color(us))
并且piece.color !== UNION
Hence my erroneous first attempt 因此,我错误的第一次尝试
Why do I get the error in the second case, yet the first case included that same property?
为什么在第二种情况下却出现错误,而第一种情况却包含相同的属性?
Boolean operators are short circuiting . 布尔运算符是短路的 。 That means for
这意味着
a || b
a || b
: b
is only evaluated if a
is false
a || b
:仅当a
为false
时才评估b
a && b
: b
is only evaluated if a
is true
a && b
:仅当a
为true
时才评估b
So, piece.color !== us
is only executed if piece == null
is false
, ie piece
is not null
or undefined
. 因此,
piece.color !== us
仅在piece == null
为false
,即piece
不为null
或undefined
。 That means accessing piece.color
will always "work" (it won't throw an error). 这意味着访问
piece.color
将始终“起作用”(不会引发错误)。
On the other hand, piece.color !== UNION
is executed if piece == null
is true
. 另一方面,如果
piece == null
为true
则执行piece.color !== UNION
。 What you wrote basically means "if piece
is null and piece.color
is not UNION
. That doesn't make much sense though (because piece
being null
implies that piece.color
doesn't exist). 您写的基本上是指“如果
piece
为null,而piece.color
不是UNION
。尽管那样没有多大意义(因为piece
为null
意味着piece.color
不存在)。
I want to run
continue;
我要继续跑步
continue;
in situations when(piece.color == null || piece.color == swap_color(us))
and whenpiece.color !== UNION
在以下情况下:
(piece.color == null || piece.color == swap_color(us))
并且piece.color !== UNION
The "and" in this sentence translates to a boolean OR. 这句话中的“和”转换为布尔OR。 But you still have to check that
piece
is not null
and you can simplify the the expression because piece.color !== UNION
implies piece.color == swap_color(us)
. 但是您仍然必须检查
piece
不为null
并且可以简化表达式,因为piece.color !== UNION
意味着piece.color == swap_color(us)
。
if (piece == null || piece.color !== UNION) {
continue;
}
If piece
is null then piece == null || piece.color !== us))
如果
piece
为null,则piece == null || piece.color !== us))
piece == null || piece.color !== us))
will be true without checking piece.color !== us
, so piece.color !== UNION
will be evaluated but because piece
is null piece.color
is undefined. piece == null || piece.color !== us))
将为true而不检查piece.color !== us
,所以piece.color !== UNION
进行评估,但因为piece
是空的piece.color
未定义。
Your logic is faulty in this way. 这样您的逻辑是错误的。 So faulty in fact that I'm not sure what you mean.
实际上如此错误,以至于我不确定您的意思。 It would seem that you mean;
看来您的意思是; what do you want to happen if
piece
is null? 如果
piece
为空,您想怎么办?
This happens because of something called short-circuit evaluation : 发生这种情况的原因是所谓的短路评估 :
Let's assume piece == null
: in the first case, the compiler only needs to evaluate the left side of the or
. 让我们假设
piece == null
:在第一种情况下,编译器仅需要评估or
的左侧。 Upon getting true
, it can skip the right side, since the entire expression will be true
anyway. 一旦为
true
,它就可以跳过右侧,因为无论如何整个表达式都是true
的。
In the second case, the left expression evaluates to true
like above, but then the compiler attempts to evaluate the right side, since an and
demands both sides to be true
. 在第二种情况下,如上所述,left表达式的计算结果为
true
,但是编译器尝试评估右侧,因为an and
要求双方均为true
。 That's where it encounters the non-existing property. 那就是它遇到不存在的属性的地方。
Try this: 尝试这个:
if (piece === null || (piece && piece.color && piece.color !== us && piece.color !== UNION)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.