Object.prototype.prefixKeys = function (prefix) {
for (var key in this)
this.hasOwnProperty(key)
&& Object.defineProperty(this, prefix + key, {value: this[key]})
&& delete this[key]
}
上面的代码在 chrome 控制台中按预期工作。但只是删除了 Node v6.10.2 中的 key 。我做错了什么?
最佳答案
前缀键仍然存在于对象上,但是如果您希望它们在您说console.log(obj)
时显示,那么您需要使属性可枚举
:
Object.prototype.prefixKeys = function (prefix) {
for (var key in this)
this.hasOwnProperty(key)
&& Object.defineProperty(this, prefix + key, {
value: this[key],
enumerable: true
})
&& delete this[key]
}
关于javascript - 使用 defineProperty 重命名的 JS 键在 Node.js 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43976077/