我以为我已经读过在将来的Javascript版本中delete()
被弃用了。
是否有人知道delete
的曾经和未来状态?
答案 0 :(得分:3)
我认为Mozilla JavaScript引用非常可靠。有一个specification部分表示 delete 运算符已在ECMAScript 1st Edition中定义,它也在ECMAScript 2016 Draft (7th Edition, ECMA-262)的最后一个草稿中。
答案 1 :(得分:2)
delete
运算符没有太大变化。截至2017年,它仍然完全有效。这是es specs:
简而言之,delete
关键字仅适用于引用,并不适用于函数,变量或函数的参数。
function deleteObjectProperty() {
var x = {a: 'deleteMe'};
var y = [1,2,3];
console.log(x.a); // Returns 'deleteMe'
delete x; // This will return 'false', deletion is not successful
console.log(x.a); // Still returns 'deleteMe'
delete x.a; // 'true', deletion is successful
delete x.b; // 'true', deletion is successful
console.log(x.a) // 'null'
delete y[0]; // 'true', array is now [empty, 2, 3]
}
function invalidDeletion(arg1) {
var x = {a: 'deleteMe'};
Object.defineProperty(x, "nonConfigurableProperty", {
value: 101,
configurable: false
});
/* These dont work */
delete x; // deleting variable
delete deleteObjectProperty; // deleting function
delete arg1; // deleting arguments
delete x.nonConfigurableProperty; // deleting non-configurable property
}
function invalidDeletionWithStrict() {
'use strict';
var x = {a: 'deleteMe'};
Object.defineProperty(x, "nonConfigurableProperty", {
value: 101,
configurable: false
});
/* SyntaxError is thrown here */
delete x; // deleting variable
delete deleteObjectProperty; // deleting function
delete arg1; // deleting arguments
/* TypeError is thrown here */
delete x.nonConfigurableProperty; // deleting non-configurable property
}