The most complete (and correct) way of doing this would be, I believe:(我认为,最完整(最正确)的方法是:)
if (old_key !== new_key) {
Object.defineProperty(o, new_key,
Object.getOwnPropertyDescriptor(o, old_key));
delete o[old_key];
}
This method ensures that the renamed property behaves identically to the original one.(此方法确保重命名的属性的行为与原始属性相同 。)
Also, it seems to me that the possibility to wrap this into a function/method and put it into Object.prototype
is irrelevant regarding your question.(另外,在我看来,将其包装到函数/方法中并将其放入Object.prototype
与您的问题无关。) 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…