johnllao

September 22, 2011

Javascript OOP Pattern Inspired By JQuery

Filed under: Uncategorized — johnllao @ 3:41 pm

I was looking at the JQuery and SignalIR sourcecode and I can say the I was intrigued by how they implemented a OOP Pattern. so I created my own simple object to show this

var Andromeda = (function() {
    // Factory method
    var Andromeda = function () {
        return new Andromeda.fn.init();
    }

    Andromeda.fn = {
        // Constructor
        init : function() {

        },

        property : 0,

        method : function() {
            var me = this;
            alert(me.property);
        }
    };

    Andromeda.fn.init.prototype = Andromeda.fn;

    return Andromeda;
}());

To call methods

 

var a = Andromeda();
a.property = 1;
a.method();

var b = Andromeda();
b.property = 2;
b.method();

Happy coding…

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Shocking Blue Green. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.