-5

该代码用于验证图像或单元格选择。我的问题是什么是 !== 在以下函数中使用:

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"
}
4

2 回答 2

9

!==是一个更严格的不等式,它不会对操作数执行任何类型强制,相比之下!=,它会执行类型强制.

!==如果操作数不相等和/或类型不同,So将返回 true。

相反!=,如果操作数相等,则返回 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.

有关更多信息,请查看MDN 上的比较运算符

于 2013-04-23T18:01:06.287 回答
5

它的意思是“不严格等于”,而不是!=“不等于”。

有两种检查相等性的方法:=====,分别是“相等”和“严格相等”。要查看确切的差异,请查看此表

!=并且!==只是这些操作的相应否定。所以例如a !== b是一样的!(a === b)

于 2013-04-23T18:00:57.647 回答