Javascript Style Reference

Writing proper Javascript is not quite intuitive (at least for me), so I’m recording these two patterns for creating a module and a class here for future reference:

myModule = function () {

    var myPrivateVar;
    var myPrivateMethod = function () { }

    return  {
        myPublicProperty: "something",
        myPublicMethod: function () { }
    };
}();

function myClass() {
	var  private_var = 1;
	var  private_function = function() { alert("private!"); };
	this.public_var    = "something";
	this.public_method = function() { alert(private_var); private_var += 1; }
}

var myInstance = new myClass();

Leave a Reply