【问题标题】:Adding functionality to Intl.NumberFormat向 Intl.NumberFormat 添加功能
【发布时间】:2017-12-06 12:07:37
【问题描述】:

我正在尝试向format function 添加功能,但我的代码有问题:

Object.defineProperty(Intl.NumberFormat.prototype, "format", { value: function(){
     //your logic here
     let orig = Intl.NumberFormat.prototype
     console.log(orig);// does not remember the original proto
}, configurable: true } );

我错过了什么?

【问题讨论】:

  • “原始原型”是什么意思?你在期待什么?

标签: javascript prototype ecmascript-intl


【解决方案1】:

您基本上可以捕获属性本身。你想得到原始的,所以在它被覆盖之前,你也可以通过复制它们来存储它的子对象引用:

{
   let orig = Object.assign({}, Intl.NumberFormat.prototype);
   Object.defineProperty(Intl.NumberFormat.prototype, "format", { value: function(){
      //your logic here     
     console.log(orig);// does remember the original proto
   }, configurable: true } );
}

【讨论】:

  • 感谢您的回复,但似乎 orig.format 正在重新写入新声明
  • @roni gadot 在这种情况下您需要保留参考资料,已编辑
猜你喜欢
  • 2010-09-07
  • 1970-01-01
  • 2012-07-22
  • 2013-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-27
相关资源
最近更新 更多