Search code examples
javascriptdollar-sign

What is the outcome in javascript with multiple librarys that use $


Let's pretend this is in the <head> of your html page.

OOPS this was a bit that was missing before...:

<script type="text/javascript" src="/include/js/billys.js"></script>
<script type="text/javascript" src="/include/js/susies.js"></script>
<script type="text/javascript" src="/include/js/marys.js"></script>

Order of the 3 scripts could vary. What would be the outcome?

Billy defines $ as

function $ () {
 return false;
}

Susie defines $ as

function $ () {
 return document.getElementById('body');
}

Mary defines $ as

function $ () {
 alert('I wrote this');
}

Solution

  • Whatever is last is the final definition of $

    That is why in (for example) jQuery there is noConflict() which lets you use a different variable than $ for jQuery