什么是!==用javascript?
该代码用于验证图像或单元格选择。我的问题是 什么是!==用于以下功能中:什么是!==用javascript?,javascript,Javascript,该代码用于验证图像或单元格选择。我的问题是 什么是!==用于以下功能中: function checkSelected() { var cellList, selectedCell, imgList, selectedImg, i cellList = document.getElementById("imgTable") cellList = cellList.getElementsByTagName("
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"
}
它的意思是“不完全等于”,而不是=代码>表示“不等于”
检查是否相等有两种方法:=
和=
,分别是“相等”和“严格相等”。要查看确切的区别,请查看
=代码>和==代码>只是这些操作的相应否定。例如a!==b
与相同!(a==b)
==与相比,code>是一个更严格的不等式,它不会对操作数执行任何类型强制=代码>,它执行类型强制
所以==如果操作数不相等和/或不属于同一类型,code>将返回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的内容。我明白了,但是,它在IF语句中的用途是什么?它是否用于验证Null?我在您共享的代码中看到它的唯一位置是.indexOf(“7px开始”)!==-1
。这意味着“如果.indexOf
的值不是-1
(使用该参数调用),则给出true
。我们知道。字符串上的indexOf
将具有类型编号,因此我们可以愉快地使用!=-1
,因为-1
也是一个数字。根据Paul S所说的,您可能感兴趣的是-1==”-1“
为真,而-1==”-1“
为假。