简体   繁体   English

属性被标记为未定义,即使它在同一行上被完美使用?

[英]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可能是以下三种情况之一

  • null 空值
  • us 我们
  • them (which is the same as swap_color(us) 它们(与swap_color(us)相同
  • UNION 联盟

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 :仅当afalse时才评估b
  • a && b : b is only evaluated if a is true a && b :仅当atrue时才评估b

So, piece.color !== us is only executed if piece == null is false , ie piece is not null or undefined . 因此, piece.color !== us piece == nullfalse ,即piece不为nullundefined 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 == nulltrue则执行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 。尽管那样没有多大意义(因为piecenull意味着piece.color不存在)。


I want to run continue; 我要继续跑步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

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.

相关问题 Angular2 - 即使设置了类属性,它也未定义 - Angular2 - Class property is undefined even though it is being set 即使我刚刚使用它,FB仍未定义 - FB is undefined even though I just used it 未捕获的类型错误:无法读取未定义的属性“焦点”,即使正在调用焦点 - Uncaught TypeError: Cannot read property 'focus' of undefined, even though focus is being called 即使属性存在,js对象也返回undefined - js object returns undefined even though property is there 即使值存在,属性也会返回未定义…? - Property returns undefined even though the value exists…? 即使属性未标记为只读,打字稿“无法分配给属性,因为它是常量或只读” - Typescript “Cannot assign to property, because it is a constant or read-only” even though property is not marked readonly 离子3 - 所有进口都是未使用的警告(即使它们被使用) - Ionic 3 - All imports are unused warning (even though they are being used) 对象属性返回 undefined 即使它存在 JavaScript - object property returns undefined even though it exists JavaScript 即使已导出模块,也无法设置未定义的属性“大小” - Cannot set property 'size' of undefined even though module was exported “无法读取未定义的属性‘setState’”,即使它已绑定 - "Cannot read property 'setState' of undefined" even though it is bound
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM