Javascript If语句测试失败

Javascript If语句测试失败,javascript,Javascript,测试从document.images[].src派生的字符串中的最后6个字符。我的“if”条件语句总是通过测试,即使它们应该失败。变量“test_result”在函数末尾的值始终为3。我尝试在条件语句中使用“==”,结果相同。谢谢你的帮助 function test_it() { var test_result = 0; var test1 = document.images[0].src; test1 = test1.substring(test1.length, test1

测试从document.images[].src派生的字符串中的最后6个字符。我的“if”条件语句总是通过测试,即使它们应该失败。变量“test_result”在函数末尾的值始终为3。我尝试在条件语句中使用“==”,结果相同。谢谢你的帮助

function test_it()
{
   var test_result = 0;
   var test1 = document.images[0].src;
   test1 = test1.substring(test1.length, test1.length - 6);
   if (test1 = "52.gif")
   {
     test_result ++
   }

   var test2 = document.images[1].src;
   test2 = test2.substring(test2.length, test2.length - 6);
   if (test2 ="48.gif")
   {
      test_result ++
   }

   var test3 = document.images[2].src;
   test3 = test3.substring(test3.length, test3.length - 6);
   if (test3 = "47.gif")
   {
      test_result ++
   }

   if (test_result = 3)
   {
      document.getElementById("ta").value = test1 + " " + test2 + " " + test3 + " " +     test_result
   }
   else
   {
      return
   }
}

if语句中,将
=
替换为
=

有一种好的(或不好的)编码方式可以用来编写,如果称之为条件的话,它可以帮助:

而不是写:

if (test1 === "52.gif")
写:

if ("52.gif" === test1)

现在,如果您不小心使用赋值运算符而不是相等运算符,您将得到一个
引用错误

=
运算符用于为变量赋值,请使用
=
==
来比较两个值。

必须使用相等或严格相等比较运算符,即分别为
=
==
。有关差异的详细说明,请参见:

相等(=)

如果两个操作数的类型不同,JavaScript将转换 然后应用严格比较。如果任一操作数为 如果是数字或布尔值,则操作数将转换为数字 可能的否则,如果任一操作数为字符串,则字符串操作数为 如果可能,转换为数字。如果两个操作数都是对象,则 JavaScript比较操作数相同时的内部引用 引用内存中的同一对象

严格相等(==)

如果操作数严格相等(请参见上文),且没有 类型转换

identity(
==
)运算符的行为与equality(
=
)运算符的行为相同,只是没有进行类型转换,并且类型必须相同才能视为相等

参考:Javascript教程:

执行任何必要的类型转换后,
=
运算符将进行相等性比较。
==
运算符不会进行转换,因此,如果两个值的类型不同,则===只会返回false。在这种情况下,===将更快,并且可能返回与==不同的结果。在所有其他情况下,性能将相同

引用Douglas Crockford优秀的JavaScript:The Good Parts

JavaScript has two sets of equality operators: `===` and `!==`, and their evil twins `==` and `!=`. The good ones work the way you would expect. If the two operands are of the same type and have the same value, then `===` produces true and `!==` produces false. The evil twins do the right thing when the operands are of the same type, but if they are of different types, they attempt to coerce the values. the rules by which they do that are complicated and unmemorable. These are some of the interesting cases:

'' == '0'           // false
0 == ''             // true
0 == '0'            // true

false == 'false'    // false
false == '0'        // true

false == undefined  // false
false == null       // false
null == undefined   // true

' \t\r\n ' == 0     // true
The lack of transitivity is alarming. My advice is to never use the evil twins. Instead, always use === and !==. All of the comparisons just shown produce false with the === operator.
来源:

使用==(双重相等-必要时进行类型转换)或==(双重相等-不进行类型转换)

请参阅此链接:


您的测试条件始终为真,并且您测试了3次,那么测试结果最终将始终为3

=运算符将值分配给变量,因此

if (test1 = "52.gif")
{
  test_result ++
}

test1 = "52.gif" ;
if (test1) // Always true
{
  test_result ++
}
要按值进行比较,而不考虑需要使用==“等于”运算符的类型。使用此运算符时,(1==1)等表达式为真,(1==“1”)也为真。要强制类型检查,则需要使用===运算符,然后(1===1)为true,但(1===“1”)为false

将if语句更改为:

if (test1 == "52.gif")
{
  test_result ++
}

您正在使用赋值运算符。试试text1='52.gif'好的,对不起,我会在其他人之前写答案。。但我保存了答案之后。。。都是。。。你有问题吗?准确的答案。非常感谢。我在条件上试过==但在“test_result”的最终测试中没有。即使我认为在这种情况下这可能是一个很好的建议,我也不同意“始终使用”部分。如果您确信您正在比较相同类型的值,最好使用您命名的“邪恶双胞胎”,或者当您不知道或不关心给定给函数的参数类型时,例如函数ifFive(x){If(x==5)doStuff();};将在ifFive(myInput.value)中失败;即使值是5,这确实是您想要的,而不是类型本身。感谢您的输入。+1推荐Yoda条件,-1没有警告持续解析为false的副作用。您的回答帮助我重新思考。谢谢你的胡安。你的回答非常清楚和正确。