I'm creating a javascript object that will store information about the user logged into my site (for easy access), but for me, It's been very difficult to understand the ways of creating an object in javascript. I've seen examples using the prototype way, and anothers using the closures way, I decided to stay with the closures, because I'll need only a single instance of this object, and so, don't is a big overhead.
I wonder if what I'm doing is correct, and if there is any way to improve my code, this is my code:
(function(window){
var mysite = (function() {
var me = this;
return { //public interface
init : function(userInfo){
me.user = userInfo;
return this;
},
sayHello : function(){
return 'Hello, my name is ' + me.user.name + ' and I am ' + me.user.age + ' years old.';
}
}
}());
window.mysite = function(userInfo){
return mysite.init(userInfo);
}
})(window);
var mysite = mysite({name : 'Jonathan', age : 17});
mysite.sayHello();
Edit #1
If I would like to add sub-objects to the main object MySite, and these sub-objects have their own methods and properties, as well as access to properties and methods of the main object (MySite), I'd like to do something like this:
mysite.timezone.calculeUserTimezone();
How to proceed?