简体   繁体   English

Javascript:删除运算符

[英]Javascript: Delete operator

I am looking at the Mozilla Developers website on the concept of the delete operator. 我正在看有关删除运算符概念的Mozilla开发人员网站。 In the last sub section of the page referring to “Deleting array elements” two similar scripts are shown, but the only difference in the scripts is how they modified the array. 在页面的最后一个子节中,“删除数组元素”显示了两个相似的脚本,但是脚本之间的唯一区别是它们如何修改数组。

In the first script, I quite don't understand why “if” statement does not run. 在第一个脚本中,我完全不理解为什么“ if”语句不运行。 My current understanding is that delete operator “removes the element of the array”. 我目前的理解是delete运算符“删除了数组的元素”。 If I were to type trees[3] in the console, it would return undefined in the console. 如果要在控制台中键入trees [3],它将在控制台中返回undefined。

 var trees = ["redwood","bay","cedar","oak","maple"]; delete trees[3]; if (3 in trees) { // this does not get executed } 

The second script seems to "mimic" the delete, but not literally. 第二个脚本似乎是“模仿”了删除操作,但实际上并非如此。 Undefined is assigned to trees[3]. 未定义分配给树[3]。 It doesn't make sense to me how the “if” block runs in this script, but the first example does not. 对我而言,“ if”块在此脚本中如何运行没有意义,但第一个示例却没有。 Can anyone help me understand this JavaScript behavior? 谁能帮助我了解这种JavaScript行为?

 var trees = ["redwood","bay","cedar","oak","maple"]; trees[3] = undefined; if (3 in trees) { // this gets executed } 

In your second example the key 3 still exists. 在第二个示例中,键3仍然存在。 It just holds a value that happens to be undefined. 它只包含一个未定义的值。 It IS confusing, but that's just the way Javascript is. 这很令人困惑,但这就是Javascript的方式。 The in operator just checks if the key exists, not if the value is defined. in运算符仅检查键是否存在,而不检查值是否已定义。

If you were to output the whole arrays after each of your "deletions" the first example would display something like this: 如果要在每个“删除”之后输出整个数组,则第一个示例将显示如下内容:

["redwood", "bay", "cedar", 4: "maple"]

Whilst the second example would print out something like this: 虽然第二个示例将打印出如下内容:

["redwood", "bay", "cedar", undefined, "maple"]

So as you can see, in your first example the key is completely missing and it continues with the next key which is 4. In the second example the key still exists, but it's value is set to undefined. 如您所见,在您的第一个示例中,密钥完全丢失,并继续使用下一个密钥4。在第二个示例中,密钥仍然存在,但是其值设置为undefined。

There is a huge difference between the two methods you are trying: 您尝试使用的两种方法之间存在巨大差异:

Method 1: 方法1:

You are deleting, destroying, completely removing the key 3 in your array called tree , hence there is no 3 in tree left, and the if check returns false . 您正在删除,销毁并完全删除名为tree的数组中的键3 ,因此tree中没有3 in tree ,并且if check返回false

Method 2: 方法2:

You are assigning a new value to the key 3 , which is undefined , there is still 3 in tree , and the if check returns true . 您正在为键3分配一个新值,该键是undefined3 in tree仍然有3 in tree ,并且if支票返回true

There is a difference between undefined which is set by the user and undefined which the javascript engine returns once something is actually undefined, meaning doesn't exist. 由用户设置的undefined和undefined之间的区别是,当实际上未定义某些内容(即不存在)时,javascript引擎将返回undefined。 javascript can tell the difference between the two. javascript可以分辨出两者之间的区别。

So in your example, when you do this: 因此,在您的示例中,当您执行以下操作时:

var trees = ["redwood","bay","cedar","oak","maple"];
trees[3] = undefined;
if (3 in trees) {
    console.log("hi");
}

javascript can tell that property 3 exists, but it was set to undefined by the user. javascript可以告诉您存在属性3,但用户已将其设置为undefined。

to prove so you have the following: 为了证明您具有以下条件:

if (5 in trees) {
    console.log("hi");
}

the property 5 of the array was never created, javascript knows it's undefined by lack of creation and regards it as a property which doesn't exist, and therefore doesn't display the "hi" 数组的属性5从未创建过,javascript知道由于缺少创建而未定义,因此将其视为不存在的属性,因此不会显示“ hi”

if(3 in tree) { //Stuff that won't get executed } is in fact correct, the thing is that in operator in Javascript does not work like in python, it simply checks if an object has a proprety. if(3 in tree) { //Stuff that won't get executed }的东西if(3 in tree) { //Stuff that won't get executed }实际上是正确的,问题在于Java语言中的in运算符不能像python中那样工作,它只是检查对象是否具有属性。 An array in javascript has a proprety 0, just like a string has a proprety 2 with the value someString[2]. javascript中的数组的属性为0,就像字符串的属性为2一样,其值是someString [2]。 the difference between delete object[prop]; 删除对象[prop]之间的区别; and object[prop] = undefined; 和object [prop] =未定义; can be seen through object.hasOwnProperty(prop); 可以通过object.hasOwnProperty(prop);看到 or iterating through values or props of the object. 或遍历对象的值或道具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM