Object.prototype.prefixKeys = function (prefix) {
for (var key in this)
this.hasOwnProperty(key)
&& Object.defineProperty(this, prefix + key, {value: this[key]})
&& delete this[key]
}
Code above works as expected in chrome console. But just removes keys in Node v6.10.2. What am I doing wrong?
The prefixed keys still exist on the object, but if you want them to show up when you, say console.log(obj)
, then you need to make the properties 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]
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.