Javascript如果语句测试失败

时间:2014-03-13 20:31:40

标签: javascript

测试从document.images []。src派生的字符串中的最后6个字符。我的"如果"条件语句总是通过测试,即使它们应该失败。变量" 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
   }
}

6 个答案:

答案 0 :(得分:1)

标识(===)运算符的行为与相等(==)运算符的行为相同,但不进行类型转换,并且类型必须相同才能被视为相等。

参考:Javascript教程:Comparison Operators

==运算符将在执行任何必要的类型转换后比较相等性。 ===运算符不会进行转换,因此如果两个值不相同,则===将返回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.

来源:Which equals operator (== vs ===) should be used in JavaScript comparisons?

答案 1 :(得分:0)

=替换为 if 语句中的===

编写 if 条件称为YODA Notation可能会有所帮助的好(或坏)编码风格:

而不是写作:

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

写:

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

现在,如果您不小心使用赋值运算符而不是使用相等运算符,那么您将获得ReferenceError

答案 2 :(得分:0)

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

答案 3 :(得分:0)

您必须分别使用相等或严格相等比较运算符,即=====。可以在Comparison Operators找到对差异的详细描述:

  

等于(==)

     

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

     

严格相等(===)

     

如果操作数严格相等(见上文),则返回true   类型转换。

答案 4 :(得分:0)

使用==(双重等于 - 必要时进行类型转换)或===(tiple equal - 无类型转换)

请看这个链接:

Which equals operator (== vs ===) should be used in JavaScript comparisons?

答案 5 :(得分:0)

您的测试条件始终为真,并且您正在测试3次,然后test_result将始终为3。

=运算符将值赋给变量,所以

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

相同
test1 = "52.gif" ;
if (test1) // Always true
{
  test_result ++
}

要按值进行比较而不管您需要使用的类型==“等于”运算符。使用此运算符,表达式(1 == 1)为真,(1 ==“1”)也为真。要强制进行类型检查,那么你需要使用===运算符,然后(1 === 1)为真,但(1 ===“1”)为假。

将您的if语句更改为:

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