2

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?

2 Answers 2

1

Using the prototype

(function(window) {
    var MySite = function(opt) {
        this.user = opt;
    };

    MySite.prototype.sayHello = function() {
        return 'Hello, my name is ' + this.user.name + ' and I am ' + this.user.age + ' years old.';
    };

    window.mysite = function(options) {
        return new MySite(options);
    }
})(window);

var a = mysite({
    name: 'Jonathan',
    age: 17
});
console.log(a.sayHello());

I would go for the prototypical approach instead. Your not taking advantage of closures so this way is neater.

Live Example

Using closures

(function(window) {
    var MySite = function(opt) {
        var user = opt;

        this.sayHello = function() {
            return 'Hello, my name is ' + user.name + ' and I am ' + user.age + ' years old.';
        }
    };

    window.mysite = function(options) {
        return new MySite(options);
    }
})(window);

var a = mysite({
    name: 'Jonathan',
    age: 17
});
console.log(a.sayHello());

Live Example

Sign up to request clarification or add additional context in comments.

8 Comments

Your not taking advantage of closures Could you explain this better? Following the example you posted, if I wanted the user to be private property, how would I do? Thanks.
@Jonathan there is no such thing as private in JavaScript ;) I also forgot to mention that your code was a single object that keeps being re-initialized and doesn't allow you to create multiple instances.
But in my example me.user can't be acessed outside the closure.
@Jonathan and why does that matter?
Because I wouldn't want that a user with firebug has easy access to this property, I know that javascript is in practice, totally public, but I want to hinder access to this property. Now one last question, following the two examples you gave, if I had another object (inside mySite), with its own functions and properties, how could I do?
|
0
   var mysite = (function() {
        var me = this;

        return { //public interface
            init : function(userInfo){
                me.user = userInfo;
                return this;
            },
            sayHello : function(){
                return alert(me.user.name);
            }
        }
    })();


mysite.init({name : 'Jonathan', age : 17});
mysite.sayHello();

I think this will also be good, because mysite will be a single object which can be accessed everywhere.

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.