2013-04-23 51 views
-10

該代碼用於驗證圖像或單元格選擇。我的問題是 什麼是==在下面的函數用於:什麼是!==在JavaScript中?

function checkSelected() { 
    var cellList, 
     selectedCell, 
     imgList, 
     selectedImg, 
     i 

    cellList = document.getElementById("imgTable") 
    cellList = cellList.getElementsByTagName("td") 

    imgList = document.getElementById("b_list") 
    imgList = imgList.getElementsByTagName("img") 

    if (cellList[0].style.border.indexOf("7px outset") !== -1) { selectedCell = cellList[0] } 


    if (selectedCell === undefined || selectedImg === undefined) { return } 

    selectedCell.style.backgroundImage = "url(" + selectedImg.src + ")" 
    selectedCell.firstChild.style.visibility = "hidden" 

    selectedCell.style.border = "1px solid" 
    selectedImg.style.border = "1px solid" 
} 
+0

http://stackoverflow.com/a/523647/384155 – Osiris 2013-04-23 18:01:32

回答

2

,意思是「不嚴格等於」,而不是!=,這意味着「不等於」。

檢查等號的方法太多了:=====,它們分別是「等於」和「嚴格等於」。要查看確切的差異,請查看this table

!=!==只是這些操作的相應否定。因此,例如a !== b相同!(a === b)

+0

我明白了,但是,它在IF語句中用到了什麼?它用於驗證Null嗎? – 2013-04-23 18:08:02

+0

我在共享的代碼中看到它的唯一地方是.indexOf(「7px outset」)!== -1'。這意味着「如果'.indexOf'的值不是'-1'(用該參數調用),則給出'true'。我們知道字符串上的'.indexOf'將具有_Number_類型,因此我們可以愉快地使用'!= = -1',因爲'-1'也是_Number_。 – 2013-04-23 18:29:47

+0

要建立在Paul S所說的內容上,你可能會感興趣的是'-1 ==「-1」'是** true **, -1 ===「-1」是** false **。 – Cam 2013-04-23 20:08:39

3

!==是更嚴格的不等式不對操作數執行任何類型的脅迫,相比!=,這並執行類型強制。如果操作數不相同和/或不是相同類型,則!==將返回真。如果操作數相等,則!=返回true。如果操作數的類型不同,JavaScript會嘗試將兩個操作數轉換爲適合比較的形式。如果操作數是對象,那麼JavaScript會比較它們的引用(內存地址)。

"3" != 3; // returns false because JavaScript will perform 
      // type coercion and compare the values 

"3" !== 3; // returns true because the first operand is a 
      // string whereas the second is an integer. Since 
      // they are of different types, they are not equal. 

欲瞭解更多信息,看看Comparison Operators上MDN:這是如下最好的證明。