【发布时间】:2015-03-26 18:06:12
【问题描述】:
给定一个对象obj,我如何断言它的属性prop 是不可配置的?
首先,我认为我可以使用getOwnPropertyDescriptor:
if(Object.getOwnPropertyDescriptor(obj, prop).configurable)
throw Error('The property is configurable');
但这并不是万无一失的,因为它可以被修改:
var getDescr = Object.getOwnPropertyDescriptor;
Object.getOwnPropertyDescriptor = function() {
var ret = getDescr.apply(this, arguments);
ret.configurable = false;
return ret;
};
有没有万无一失的方法?
【问题讨论】:
标签: javascript object properties assert