2

I'm fairly inexperienced with javascript but I'm trying to create a library for people to use. Supposed I set the function window.onerror to my own function in my own js file but someone else also assigns window.onerror to a function in their code.

If they execute their js code before mine, won't my window.onerror function overwrite theirs? And if they execute their js code after mine, won't the opposite happen? Now, window.onerror is just example but if I were to use jquery in my code for something like $.ajaxComplete that have a global scope, is there a way to accommodate both our methods without knowing the order?

wesshi
  • 231
  • 2
  • 11
  • Hava a look at require.js (http://requirejs.org/) - if you use its modular approach, you can avoid most conflicts. It is JQuery compatible. – tucuxi Aug 29 '14 at 16:15
  • 4
    Implement a [namespace](http://stackoverflow.com/questions/881515/javascript-namespace-declaration) – George Cummins Aug 29 '14 at 16:15
  • 3
    Most (or all) event's have a `addEventListener` counterpart which should solve the `onxxx` issue completely – Torben Aug 29 '14 at 16:26
  • 1
    +1 for having the foresight to (correctly) foresee this problem and being smart enough to ask about it. – pwdst Aug 29 '14 at 16:54

1 Answers1

0

Only global variables and functions will have this problem (window is a global variable). I don't see why you need to have a general error for your whole library, how about a specific error callback for each thing that could go wrong.

For this, I would recommend jQuery Deferred, there is a great tutorial on it at http://joseoncode.com/2011/09/26/a-walkthrough-jquery-deferred-and-promise/.

Hope this helps.

Zak
  • 1,910
  • 3
  • 16
  • 31