JavaScript的`delete`运算符没有按预期工作

时间:2016-09-23 12:21:54

标签: javascript

我有一个如下的对象:

var doc = {
  local: {
    username: 'admin',
    status: true,
    password: '$2a$08$81Kq7/59ZASHl5k9n5cNvOUSl/HYgpLKZjs66P/OJV8vw.nHW60Ta',
    lastname: 'admin',
    firstname: 'admin',
    email: 'admin@gmail.com',
    datetime_modified: 'TueAug30201623: 10: 56GMT+0530(IST)',
    datetime_created: 'MonAug29201622: 34: 12GMT+0530(IST)'
  },
  roles: [
    'ADMIN',
    'HR'
  ],
  GridOrder: [
    {
      _id: '57e507fce94094be085425c9',
      domainName: 'items'
    }
  ],
  _id: '57c46b0c12907af910167ff5',
  __v: 9
}

现在我想从“doc”中删除属性“GridOrder”。 我尝试使用“删除”,如下所示:

delete doc.GridOrder

,如下所示:

delete doc['GridOrder']

但没有任何事情发生。我也没有收到任何错误。

如何从“doc”中删除“GridOrder”。有什么想法吗?

3 个答案:

答案 0 :(得分:2)

使对象属性可配置

使用上述代码时,您的最小示例按预期工作。

根据您的评论,GridOrder实际上是使用Object.defineProperty()定义的,默认情况下会创建不可配置的(读取:不可删除)属性。

在定义属性configurable: true时,只需将Object.defineProperty传递给GridOrder



var doc = {
  local: {
    username: 'admin',
    status: true,
    password: '$2a$08$81Kq7/59ZASHl5k9n5cNvOUSl/HYgpLKZjs66P/OJV8vw.nHW60Ta',
    lastname: 'admin',
    firstname: 'admin',
    email: 'admin@gmail.com',
    datetime_modified: 'TueAug30201623: 10: 56GMT+0530(IST)',
    datetime_created: 'MonAug29201622: 34: 12GMT+0530(IST)'
  },
  roles: [
    'ADMIN',
    'HR'
  ],
  _id: '57c46b0c12907af910167ff5',
  __v: 9
}

// Objects defined this way cannot be deleted unless `configurable` is set to `true`
Object.defineProperty(
  doc,
  'GridOrder',
  {
    configurable: true, // Defaults to `false`
    value: [
      {
        _id: '57e507fce94094be085425c9',
        domainName: 'items'
      }
    ]
  }
);

console.log('doc.GridOrder: ', doc.GridOrder);
console.log('delete doc.GridOrder: ', delete doc.GridOrder);
console.log('doc.GridOrder: ', doc.GridOrder);




买者

  

如果configurablefalse,则默认情况下,尝试再次拨打defineProperty将导致JavaScript错误...   如果configurable设置为true,您可以稍后再次修改该属性。

Advanced objects in JavaScript

如果您无权访问使用defineProperty创建GridOrder的脚本,则以后您将无法修改或删除它。

例如,这将失败:



/*
 * Imagine this is provided by some third-party library that you're unable to edit
 */

var doc = {};

// Objects defined this way cannot be modified or deleted because `configurable` defaults to `false`.
Object.defineProperty(
  doc,
  'GridOrder',
  {
    value: [
      {
        _id: '57e507fce94094be085425c9',
        domainName: 'items'
      }
    ]
  }
);

/*
 * Within your script…
 */

console.log('doc.GridOrder: ', doc.GridOrder);
console.log('delete doc.GridOrder: ', delete doc.GridOrder);
console.log('doc.GridOrder: ', doc.GridOrder);

// …attempts to redefine non-configurable properties will fail
Object.defineProperty(
  doc,
  'GridOrder',
  {
    configurable: true
  }
);

console.log('This will never be seen because an Error has been thrown above.');




答案 1 :(得分:0)

确认您可以访问权限object并使用delete

var doc = {
  local: {
    username: 'admin',
    status: true,
    password: '$2a$08$81Kq7/59ZASHl5k9n5cNvOUSl/HYgpLKZjs66P/OJV8vw.nHW60Ta',
    lastname: 'admin',
    firstname: 'admin',
    email: 'admin@gmail.com',
    datetime_modified: 'TueAug30201623: 10: 56GMT+0530(IST)',
    datetime_created: 'MonAug29201622: 34: 12GMT+0530(IST)'
  },
  roles: [
    'ADMIN',
    'HR'
  ],
  GridOrder: [
    {
      _id: '57e507fce94094be085425c9',
      domainName: 'items'
    }
  ],
  _id: '57c46b0c12907af910167ff5',
  __v: 9
}
delete doc.GridOrder;
console.log('doc.GridOrder', doc.GridOrder)
console.log(doc)

修改

根据评论如何使用Object.defineProperty定义属性,您需要添加{configurable: true},以便{I}可以根据需要添加。{/ p>

delete会这样做

检查参考Object.defineProperty

答案 2 :(得分:0)

您应该以严格模式运行代码,在这种情况下,删除失败会导致运行时错误,从而更容易发现:

  

未捕获的TypeError:无法删除#(...)`

的属性'foo'

如果你真的想要,那么你也可以catch

在草率模式下,delete运算符将在失败时返回false,因此您可以检查其值以帮助了解发生的情况。根据MDN文档:

  对于所有情况

true,除非属性是自己的不可配置属性,在这种情况下,false以非严格模式返回。

请注意,仅仅不存在的属性是返回false或者当然失败的原因。

一位评论者声称:

  

也是对象原型链上存在的属性?在这两种情况下,删除都将失败。

不,原型上的属性会导致delete失败。它会成功(意思是不抛出,而不是返回false),但什么都不做。