使用defineProperty重命名JS key将无法在Node.js中工作

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中删除键。 我究竟做错了什么?

前缀键仍然存在于对象上,但是如果你希望它们出现在console.log(obj) ,那么你需要使属性enumerable

 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] }