Search code examples
javascriptobjectimmutabilitypolyfills

Make polyfills immutable


For example:

Object.defineProperty(Promise.prototype, 'onFinally', {
  get: () => {},
  writable: false,
});

or

Object.freeze(Promise.prototype);

These examples aren't work, is there a working way?

UPD Thank you all for your help, this method really works, but in my situation the problem is different. There is a problem on the Shopify store where Promise.prototype is somehow completely overwritten, although this is unlikely. All third-party Promise.prototype properties are deleted, possibly all properties except built-in ones are deleted via delete.


Solution

  • Actually the example you provided works:

    Object.defineProperty(Promise.prototype, 'onFinally', {
        get: () => { console.log("Retrieved.") }
    });
    
    Promise.prototype.onFinally
    
    Object.freeze(Promise.prototype);
    
    Promise.prototype.onFinally
    
    Object.defineProperty(Promise.prototype, 'onFinallySecond', {
        get: () => { console.log("Retrieved.") }
    });
    

    TypeError: Cannot define property onFinallySecond, object is not extensible

    You just have to call it in the correct order.