是Javascript'删除()'被弃用?

时间:2016-01-04 09:23:25

标签: javascript

我以为我已经读过在将来的Javascript版本中delete()被弃用了。

我记得,原因是,如果ES6对象中的属性一旦声明,就无法添加或删除,那么性能要好得多。

是否有人知道delete的曾经和未来状态?

2 个答案:

答案 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
}