0

I was wondering, if it is possible to make this code change to a point where I can just call MyModule.RED instead of having MyModule.COLORS.RED. I tried making mod the variable to store the colors but it seems to not work. Is it that I am doing it the wrong way?

(function() {
    var mod;

// Create the global, and also give ourselves a convenient alias for it (`mod`)
window.MyModule = mod = {};

// Colors
mod.COLORS = {
    RED: "#FF0000",
    BLUE: "#0000FF",
    // ...
    GREEN: "#00FF00"
};

mod.testQuery = MyModule_testQuery;
function MyModule_testQuery() {
    // Do something
}

})();

alert(MyModule.COLORS.RED); // #FF0000
 MyModule.testQuery();       // Do something

EDIT

(function() {
    var mod;

// Create the global, and also give ourselves a convenient alias for it (`mod`)
window.MyModule = mod = {};

// Colors
mod.COLORS = {
    RED: "#FF0000",
    BLUE: "#0000FF",
    // ...
    GREEN: "#00FF00"
};

var colors = mod.COLORS;

mod.testQuery = MyModule_testQuery;
function MyModule_testQuery() {
 // Do something
}

})();

alert(colors.RED); // #FF0000
 MyModule.testQuery();       // Do something
2
  • are you the same as simplified without the dot? Commented Jun 20, 2011 at 18:43
  • @Juan Mendes usually comes with the dot i suppose. not to sure if you are referring to the same person. if you are referring to a exising member on this forum then i doubt so. Commented Jun 20, 2011 at 19:05

3 Answers 3

2
// Create the global, and also give ourselves a convenient alias for it (`mod`)
var mod;
window.MyModule = mod = {
    RED: "#FF0000",
    BLUE: "#0000FF",
    // ...
    GREEN: "#00FF00"
};

or if you want to spare typing time:

var cols = mod.COLORS;
cols.RED;
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your reply. i tried changing to the answer you provided below and added it above, but it doesn't seem to work for me. can you help me see if i have missed out on anything?
issit because colors is in a local scope?
i tried making it into a global scope, but it returns undefined instead.
0

When you say "doesn't work", can you be more specific? :)

I think you're looking for this:

(function() {

// Create the global, and also give ourselves a convenient alias for it (`mod`)
window.MyModule = {
    RED: "#FF0000",
    BLUE: "#0000FF",
    GREEN: "#00FF00",
    testQuery: MyModule_testQuery
};

function MyModule_testQuery() {
   // Do something
} 

})();

alert(MyModule.RED); // #FF0000
MyModule.testQuery();       // Do something

7 Comments

@InfinitiesLoop thanks for your reply, I tried running your code, it's suppose to have an alert i suppose, but it seems that it doesn't return anything. am i missing something?
And by the way you should always look at the error console when testing. You would have seen the error in this case, for example. In JS when an error occurs, the rest of the code in that sequence will not run. One error can end up causing tons of extra errors, so when in doubt, start with the 1st error in the list.
@InfinitiesLoop hmm thanks, but i am writing these codes on a text editor. the error will come out on the browser? where can i see them? Sorry if the question sounds simple. I am new to programming you see.
Yes, in the browser. How you see it depends on the browser. In Chrome hit control-shift-j. In IE hit F12. In any case, you can usually find the 'debug console' or 'javascript console' or something to that effect in the menus for the browser.
@InfinitiesLoop sorry to trouble you again, with your solution provided above, is it possible to make the color values readonly?
|
0

Just attach the stuff directly.

(function() {
  window.MyModule = {
      RED: "#FF0000"
    , BLUE: "#0000FF"
    , GREEN: "#00FF00"
    , testQuery = function() {
        // Do something
      }
  };
})();
alert(MyModule.RED);  // #FF0000
MyModule.testQuery(); // Do something

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.