Javascript IF和OR语句

时间:2016-05-20 08:52:37

标签: javascript html

我希望有人可以提供帮助。我已经查看了堆栈上的JavaScript示例,但可以使下面的语句起作用。添加OR语句时会发生错误。有人可以帮助解决语法吗?

if (this.drawX >= CANVAS_WIDTH - this.width && this.isRightKey)
  || ((this.drawX <= -this.width && this.isLeftKey) {
    this.speed = 0;
} else {
    this.speed = 5;
}

3 个答案:

答案 0 :(得分:1)

试试这个:

if ((this.drawX >= CANVAS_WIDTH - this.width && this.isRightKey==true)
  || (this.drawX <= 0 - this.width && this.isLeft Key==true) )

    {
    this.speed = 0;
    }

    else {
    this.speed =5;
    }

你的代码问题:

1. odd no of `(` , ie. brackets are not closed properly 

2. wrong position of || condition .

3. this.isLeft Key , space issue

答案 1 :(得分:0)

你的括号已关闭。 if中的整个条件应该用括号括起来。为了提高可见性,用括号括起||的每个分支也不是一个坏主意:

if ((this.drawX >= CANVAS_WIDTH - this.width && this.isRightKey==true) || 
    (this.drawX <= 0 - this.width && this.isLeft Key==true)) {
        // code comes here...

答案 2 :(得分:0)

由于其他答案提到了您的代码问题,我想回答我的问题。您可以将其简化为:

Thread 4 name:  Dispatch queue: com.apple.root.background-qos
Thread 4 Crashed:
0   Imagina_Connect                 0x00000001002192f4 0x10008c000 + 1626868
1   Imagina_Connect                 0x0000000100218dac 0x10008c000 + 1625516
2   Imagina_Connect                 0x00000001000990d8 0x10008c000 + 53464
3   libdispatch.dylib               0x0000000182c0d4bc _dispatch_call_block_and_release + 24
4   libdispatch.dylib               0x0000000182c0d47c _dispatch_client_callout + 16
5   libdispatch.dylib               0x0000000182c1b914 _dispatch_root_queue_drain + 2140
6   libdispatch.dylib               0x0000000182c1b0b0 _dispatch_worker_thread3 + 112
7   libsystem_pthread.dylib         0x0000000182e25470 _pthread_wqthread + 1092
8   libsystem_pthread.dylib         0x0000000182e25020 start_wqthread + 4

Thread 5:
0   libsystem_kernel.dylib          0x0000000182d5cb48 __workq_kernreturn + 8
1   libsystem_pthread.dylib         0x0000000182e25530 _pthread_wqthread + 1284
2   libsystem_pthread.dylib         0x0000000182e25020 start_wqthread + 4

现在代码中出现问题:

  1. 如果条件允许var bool = (this.drawX >= CANVAS_WIDTH - this.width && this.isRightKey === true) || (this.drawX <= 0 - this.width && this.isLeftKey === true); this.speed = bool ? 0 : 5; // <=== condition ? true : false; 空缺,(也会关闭,并且您在不需要的位置)
  2. (单词之间有空格。