0

I want to initialize my javascript class like this:

Example.init({"settings": {"baseUrl":"http://example.com"}, "user": {"id":123, "name":"john doe"} );

And then I want to be able to use it like this:

Example.settings.baseUrl
Example.user.id;
Example.user.name;

I am currently using the module pattern like this:

Example = (function(){

       var _init = function(data) {

       };

       return {
           init: function($data) {
              _init($data);
           }
       };
})();

Example.module2 = (function(){

       var _init = function(data) {
          if(Example.user.id > 0) {    // referencing data set in the Example.init

          }
       };

       return {
           init: function($data) {
              _init($data);
           }
       };
})();

I'm not sure how I can expose these properties, looking for an explanation and guidance.

(please comment on best practise also, should I use $ for parameters and if so when?)

Note: Here is basic outline of what I am trying to do.

  1. The first thing I will do is call the Example.init function in all my pages:

    Example.init({"settings": {"baseUrl":"http://example.com"}, "user": {"id":123, "name":"john doe"} );

  2. I want my other modules to be able to reference this data I set in the .init() method, see the Example.module2 I added above.

Is there a timing issue with this?

Blankman
  • 259,732
  • 324
  • 769
  • 1,199
  • http://jsfiddle.net/arunpjohny/k4aowfrw/2/ – Arun P Johny Nov 20 '14 at 04:26
  • Thanks, but what if you don't use extemd, that seems like too much magic and was hoping to learn. – Blankman Nov 20 '14 at 04:29
  • you can manually copy the properties like http://stackoverflow.com/questions/9362716/how-to-duplicate-object-properties-in-another-object or http://stackoverflow.com/questions/728360/most-elegant-way-to-clone-a-javascript-object – Arun P Johny Nov 20 '14 at 04:32
  • @ArunPJohny Thanks, see my update. These properties can be visible to my other modules and I need it to be. – Blankman Nov 20 '14 at 16:27

1 Answers1

1
Example = (function(){

    var _init = function(data){
        // manually 
        //this.settings = data.settings || NULL;
        //this.user = data.user || NULL ;
        for (prop in data){
            this[prop] = data[prop];
        }
    }

    return {
           init: function(data) {
               _init.call(this, data); 
           }
       };
})();
Example.init(
    {"settings": {"baseUrl":"http://example.com"}, "user": {"id":123, "name":"john doe"}});

console.log(Example.settings.baseUrl); 
console.log(Example.user.id);
console.log(Example.user.name);

As for your other question "using $ in variable names " - It is a comment naming convention when the variable contains a Jquery object -

ratiorick
  • 572
  • 3
  • 7
  • Hi, I thought you would have to expose .settings and .user in the return statement. Thanks. – Blankman Nov 20 '14 at 15:41
  • There are a few different ways you could do it. those are being set on the object that is being returned, so they are exposed. ( forcing context when _init is being called ) – ratiorick Nov 20 '14 at 16:17
  • If I have another module like Example.someThing = (function(...)(); I can't seem to access Example.user inside of it. How can I do that b/c that was the goal of all of this for me. – Blankman Nov 20 '14 at 19:27
  • It is unclear what you are trying to do. please clarify. or provide another code example. – ratiorick Nov 21 '14 at 01:37