Javascript 简明教程
JavaScript - Delete Operator
JavaScript Delete Operator
JavaScript delete 操作符从对象中删除/移除一个属性。它从对象中移除属性以及属性的值。它仅适用于对象,不适用于变量或函数。
The JavaScript delete operator deletes/ removes a property from an object. It removes the property as well as value of the property from the object. It works only with the objects not with the variables or functions.
在 JavaScript 中,数组是一个对象,因此你可以使用“delete”操作符从特定索引中删除一个元素。然而,有 pop()、slice() 或 shift() 等方法可以从数组中删除元素。
In JavaScript, an array is an object, so you can use the 'delete' operator to delete an element from the particular index. However, there are methods like pop(), slice(), or shift() available to delete the element from the array.
Syntax
遵循下面的语法,使用“delete”操作符删除对象属性。
Follow the below syntax to delete the object property using the 'delete' operator.
delete obj.property;
OR
delete obj["property"];
Return value - 如果操作数(指定属性)被成功删除,“delete”操作符返回 true,否则如果属性未删除,则返回 false。
Return value − The 'delete' operator returns true if the operand (specified property) is deleted successfully, otherwise returns false if the property is not deleted.
如果你尝试删除一个不存在的属性,它将返回 true 但不会影响对象。
If you try to delete a property that doesn’t exist, it will return true but will not affect the object.
遵循下面的语法,使用“delete”操作符删除数组元素。
Follow the below syntax to delete the array element using the 'delete' operator.
delete arr[index];
Deleting Object Properties
JavaScript delete 操作符可用于删除对象的属性。为了删除属性,我们在对象属性之后写 delete 操作符。
The JavaScript delete operator can be used to delete a property of an object. To delete the property we write the delelte operator followed by the property of the object.
delete obj.propertyName;
or
delete obj["propertyNmae"];
在上面的语法中,名为 propertyName 的对象属性从名为 obj 的对象中被删除。
In the above syntas, object property named propertyName is being deleted from the object called obj.
Example: Deleting an Object Property
以下示例中的“obj”对象包含产品、价格和颜色属性。我们使用“delete”操作符从对象中删除价格属性。
The 'obj' object in the example below contains the product, price, and color properties. We used the ‘delete' operator to delete the price property from the object.
<html>
<body>
<div id="output"></div>
<script>
const obj = {
product: "Mobile",
price: 20000,
color: "Blue",
}
delete obj.price; // deleting price
document.getElementById("output").innerHTML =
"The Mobile price is " + obj.price + " and color is " + obj.color;
</script>
</body>
</html>
它将产生以下结果 −
It will produce the following result −
The Mobile price is undefined and color is Blue
请注意,当我们访问已删除的属性时,它返回 undefined。
Notice that when we access the deleted property, it returns undefined.
Example: Deleting a Nonexistent object Property
尝试删除一个不存在的属性。它将返回 true 但不会影响原始对象。
Try to delete a property that doesn’t exit. It will return true but doesn’t affect the original object.
<html>
<body>
<div id="output"></div>
<script>
const obj = {
product: "Mobile",
price: 20000
}
document.getElementById("output").innerHTML = delete obj.color;
</script>
</body>
</html>
上述程序会生成以下结果 −
The above program will produce the following result −
true
Deleting Array Elements
我们可以使用 delete 操作符从数组中移除或删除一个元素。为了删除一个元素,我们在数组元素后面使用 delete 关键字。我们可以使用方括号 ([]) 从数组中访问元素。
We can use the delete operator to remove or delete an element from an array. To delete an element, we use delete keyword followed by array element. We can use square brackets ([]) to access the elements from the array.
Example
下面的代码包含了一个数字数组。我们使用“delete”操作符从数组的第一个索引中删除元素。在输出中,你可以观察到数组中的该元素被删除了,但是其他元素的位置保持不变。数组长度也保持不变。
The below code contains the array of numbers. We used the 'delete' operator to delete the element from the 1st index of the array. In the output, you can observe that element from the array gets deleted, but the position of the other elements remains as it is. The array length also remains the same.
<html>
<body>
<div id="output"></div>
<script>
const arr = [10, 20, 30, 40, 50, 60];
delete arr[1]; // deleting 2nd element from array
document.getElementById("output").innerHTML =
arr + "<br>" +
arr[1];
</script>
</body>
</html>
它将产生以下结果 −
It will produce the following result −
10,,30,40,50,60
undefined
Deleting Predefined Object
JavaScript “delete”操作符可以删除预定义的对象,例如 Math、Date 等。不建议删除预定义的对象。一旦删除,你将无法访问这些对象的属性。
The JavaScript 'delete' operator can delete the predifiend object such as Math, Date, etc. It is not advisable to delete predefined objects. Once deleted, you can’t access the properties of these objects.
Example: Deleting Built-in Math Object
在下面的示例中,我们尝试删除 Math 对象,因此我们得到了上面的错误。
In the example below, we try delete Math object so we get the above error.
<html>
<body>
<div id="output"></div>
<script>
var x = 10
var fun = function(){return 20;};
document.getElementById("output").innerHTML =
"delete Math.PI :" + delete Math.PI + "<br>" +
"delete Math :" + delete Math + "<br>";
try{
document.getElementById("output").innerHTML += Math.PI;
}
catch(e){
document.getElementById("output").innerHTML += e;
}
</script>
</body>
</html>
它将生成如下输出:
It will produce the following output −
delete Math.PI :false
delete Math :true
ReferenceError: Math is not defined
Can’t Delete Variables and Functions
delete 操作符不能删除变量或函数。
The delete operator can’t delete the variables or functions.
<html>
<body>
<div id="output1"></div>
<div id="output2"></div>
<script>
var x = 10
var fun = function(){return 20;};
document.getElementById("output1").innerHTML = delete x;
document.getElementById("output2").innerHTML = delete fun;
</script>
</body>
</html>
它将产生以下结果 −
It will produce the following result −
false
false
未经 var、let 或 const 定义的变量可以被删除。此类变量被视作 window 对象的属性。
The variable defined without var, let or const can be deleted. Such a variable is treated as property of window object.
<html>
<body>
<div id="output1"></div>
<div id="output2"></div>
<script>
try{
x = 10
document.getElementById("output1").innerHTML = delete x;
document.getElementById("output2").innerHTML = x;
}catch(e){
document.getElementById("output2").innerHTML = e;
}
</script>
</body>
</html>
它将产生以下结果 −
It will produce the following result −
true
ReferenceError: x is not defined