Search code examples
javascriptprototypeecmascript-intl

Adding functionality to Intl.NumberFormat


I'm trying to add functionality to the format function but there is something wrong with my code:

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

What am I missing?


Solution

  • You basically catch the property itself. You want to get the original one so before it is overriden, and you may store its subobject references too through copying them:

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