2017-07-03 116 views
2

我试图将功能添加到format function但有什么毛病我的代码:添加功能Intl.NumberFormat

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 }); 

我缺少什么?

+1

你是什么意思“原原”?你在期待什么? –

回答

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 }); 
} 
+0

感谢您的回复,但是似乎原始格式正在被重新写入新的声明 –

+1

@roni gadot,在这种情况下,您需要保留一个引用,即编辑 –