1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2009
    Rep Power

    JavaScript Conflicts

    I have been given the task of merging all of the javascript of 3 different websites into one file.

    I think that some of the JS is conflicting, but I don't know.
    I am not that experienced with JavaScript.

    Some of the JS is using jQuery.

    I have multiple anonymous jQuery functions:
    (function ($) { 
        // Javascript code 
    Notepad++ tells me there are 7 instances of:
    (function (a)
    I am guessing there is no doubt that would cause conflict?

    What are some common things that cause conflicts in JavaScript?
  2. #2
  3. c0der
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2005
    Rep Power
    The anonymous functions are creating a container (technically, a "closure") for local variables. Variables defined using the "var" keyword within the anonymous functions (technically, "lambda functions") will not be accessible directly from outside the function, except by calling global functions defined within the same wrapper function. This is an example of what's called "lexical scope", which I suggest you google--others have explained the concept with far greater clarity than I.

    The anonymous functions exist precisely because there will be less likelihood of variable naming clashes if the library specific variables are encapsulated by a wraparound function.

    To answer your question more directly: I wouldn't worry about clashes between libraries that make ample use of anonymous wrapping functions. Chances are these libraries were defined to minimize conflict with foreign code.

IMN logo majestic logo threadwatch logo seochat tools logo