1

“Let’s say I’ve taken a native function prototype—like Array.prototype.slice or Array.prototype.indexOf and assigned it a new value. How would you get the original value back?”
Explanation : I am building third party JS widget which embeds small HTML to consumers website. We have decided not to use IFRAME so JS library directly integrated into consumers page. Widgets working fine in test environment but its breaks on some real production website. During the investigation I found, some of the native function not working as expected on those websites which I am using.

  • How to investigate that does something wrong with native functions so that I can restore some functions as explained here
  • Any better approach for third party widget development to avoid such issue
Community
  • 1
  • 1
Dipak Telangre
  • 1,792
  • 4
  • 19
  • 46
  • 2
    Possible duplicate of [Create a reset of javascript Array prototype when Array.prototype has been modified?](http://stackoverflow.com/questions/13990187/create-a-reset-of-javascript-array-prototype-when-array-prototype-has-been-modif) – JBux Feb 08 '17 at 22:22
  • It's not good practice overriding those functions!!! – ibrahim mahrir Feb 08 '17 at 22:23
  • 1
    @ibrahimmahrir We can't control execution environment for third party library – Dipak Telangre Feb 08 '17 at 22:32
  • The `prototype` property of global constructor functions is read only (that's the prototype object, not methods set on it). Saving the values of native methods before the other library loads (or copy from an iframe) and use `Function.prototype.call` or `Function.prototype.apply` to use them within your code could be a possibility. But what an ineffable mess created by the 3rd party library! – traktor Feb 09 '17 at 00:44
  • @ibrahim mahrir Thats right. But its always possible having manipulated functions through Extensions like Greasemonkey/Tampermonkey and specific userscripts, too. They can inject own code with manipulated functions, custom polyfills and so on. There is no guaranty that a buildin function is still an unmodified native function. – SammieFox Apr 16 '17 at 13:29
  • https://stackoverflow.com/questions/8580431/recovering-built-in-methods-that-have-been-overwritten – Herohtar Jan 03 '18 at 20:10
  • @DipakTelangre "*We can't control execution environment*" - sure, but this is not your fault then. Just document your library requires some standard functions and will not work if they are overwritten. It's on the website who is using your library to fix this, nothing your library could (or should) do about this. – Bergi Jul 27 '22 at 13:29

0 Answers0