尝试删除不允许的属性时引发错误

时间:2019-04-08 11:24:16

标签: javascript

因此,我知道您可以通过将对象的configurable属性设置为false来防止删除属性。但是,您从此删除操作获得的唯一反馈是显示真或假的布尔值。删除不允许的属性时,有什么方法可以立即引发错误?

const obj = {
  deleteMe: "Some text",
  dontDeleteMe: "Some other text"
};

Object.defineProperty(obj, 'dontDeleteMe', {
    value: "Some text",
    writable : true,
    enumerable : true,
    configurable : false
});


console.log(Object.keys(obj));
delete obj.deleteMe;
console.log(Object.keys(obj));
delete obj.dontDeleteMe;
console.log(Object.keys(obj));

3 个答案:

答案 0 :(得分:4)

如果添加use strict,则会引发错误:

'use strict';
const obj = {
  deleteMe: "Some text",
  dontDeleteMe: "Some other text"
};

Object.defineProperty(obj, 'dontDeleteMe', {
    value: "Some text",
    writable : true,
    enumerable : true,
    configurable : false
});


console.log(Object.keys(obj));
delete obj.deleteMe;
console.log(Object.keys(obj));
delete obj.dontDeleteMe;
console.log(Object.keys(obj));

使用Proxy

const obj = {
  deleteMe: "Some text",
  dontDeleteMe: "Some other text"
};

Object.defineProperty(obj, 'dontDeleteMe', {
    value: "Some text",
    writable : true,
    enumerable : true,
    configurable : false
});

const handler1 = {
  deleteProperty(target, prop) {
    delete target[prop];
    if (target[prop]) {
      throw new Error(`Invalid attempt to delete '${prop}' property`);
    }
    return true;
  }
};

const proxyObj = new Proxy(obj, handler1);
console.log(Object.keys(proxyObj));
delete proxyObj.deleteMe;
console.log(Object.keys(proxyObj));
delete proxyObj.dontDeleteMe;
console.log(Object.keys(proxyObj));

答案 1 :(得分:1)

您可以将"use strict";Object.defineProperty()Object.freeze()一起使用


有关详细信息,请访问MDN

'use strict';
var obj = Object.freeze({name: 'Elsa', score: 157});
delete obj.score;  // TypeError

'use strict';
var obj = {};
Object.defineProperty(obj, 'foo', {value: 2, configurable: false});
delete obj.foo;  // TypeError

'use strict';
var frozenArray = Object.freeze([0, 1, 2]);
frozenArray.pop();  // TypeError

使用您的代码:

"use strict";
const obj = {
  deleteMe: "Some text",
  dontDeleteMe: "Some other text"
};

Object.defineProperty(obj, 'dontDeleteMe', {
    value: "Some text",
    writable : true,
    enumerable : true,
    configurable : false
});


console.log(Object.keys(obj));
delete obj.deleteMe;
console.log(Object.keys(obj));
delete obj.dontDeleteMe;
console.log(Object.keys(obj));

答案 2 :(得分:1)

如果您不想使用“使用严格”(无论出于何种原因),这将是代理解决方案:

const obj = {
  deleteMe: "Some text",
  dontDeleteMe: "Some other text"
};

const deleteHandler = {
  deleteProperty(target, prop) {
    if (prop in target) {
      const deleted = delete target[prop];
      if (!deleted) {
        console.error(`deletion not allowed: ${prop}`);
      }
    }
  }
};

Object.defineProperty(obj, 'dontDeleteMe', {
  value: "Some text",
  writable: true,
  enumerable: true,
  configurable: false
});

const proxyObj = new Proxy(obj, deleteHandler);

console.log(Object.keys(proxyObj));
delete proxyObj.deleteMe;
console.log(Object.keys(proxyObj));
delete proxyObj.dontDeleteMe;
console.log(Object.keys(proxyObj));