Jump to content

IIFE overriding jQuery document ready?


Mad_Griffith

Recommended Posts

Hi, I am trying to have FILE2.JS override FILE1.JS, but can't find the right syntax to do this (please also note I should first try and modify FILE2.JS only):

 

FILE1.JS

$(document).ready(function () {

    var weare = [
        "Young and free"
    ];

});

FILE2.JS

(function (jQuery) {
    var weare = [
        "inspired by music"
    ];
})(jQuery)
Edited by Mad_Griffith
Link to comment
Share on other sites

One variable cannot override the other, they exist in two separate and unrelated scopes. Just because they have the same name doesn't mean anything, you do not have access to the local variables inside the function from anywhere except inside the function.

Link to comment
Share on other sites

By making the variable global.

 

To make a variable global, just declare it outside of any function and don't use the var keyword inside the functions.

var weare;

$(document).ready(function () {
    weare = [
        "Young and free"
    ];

});



(function (jQuery) {
    weare = [
        "inspired by music"
    ];
})(jQuery)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...