Ok. Simple and sweet. I wish I would have known about this a few years ago. I’m talking about the idea of a JavaScript function that can be used like a class. Here’s what I want:

  • Simple class interface that I can instantiate with new Classname
  • Public functions/members
  • Private functions/members
  • Priveleged functions/members
  • Static functions/members

Lately I’ve been using JavaScript functions as class objects and utilizing private, priveleged and static members and functions to my hearts content. Let me share.

Disclaimer: I don’t consider myself a JavaScript guru, nor do I think that this is the best solution evar known to mankind. If you have a snooty comment to say, please don’t leave it here. If you have a piece of constructive criticism or have better methods, do tell. I’d love to hear what you use. If your JavaScript knowledge is more l33t, then by all means, humbly share.

A Basic Class & Instance

function MyClass () {
//...
}
var oClass = new MyClass();

Public Member Variables & Functions

Public functions and variables are available to access on an instance of a class.
function MyClass () {
//..
}

//public member variable
MyClass.prototype.publicVar = "My Public Variable";

//public member function
MyClass.prototype.publicFunction = function () {
alert( this.publicVar );
}

//create an instance
var oClass = new MyClass();

//run a member function
oClass.publicFunction(); //Alert: "My Public Variable"

Private Member Variables & Functions

Private member functions and variables are hidden to outside code. Only public functions can access them.
function MyClass () {

//reference to this
var self = this;

//private member variable
var privateVar = "My Private Variable";

//public member variable
this.publicVar = "My Public Variable";

//private member function
var privateFunction = function () {

self.publicVar += " Modified By A Private Fucntion";

alert( self.publicVar );

}
}
//create an instance
var oClass = new MyClass();

//run a private member function
oClass.privateFunction(); //private function is undefined

//get a private member var
alert( oClass.privateVar ); //private var is undefined

Static Member Variables & Functions

A static function or variable is available on the base class (or JavaScript) function, but is not available to the class instance.
function MyClass () {
//...
}

//declare a static member
MyClass.staticVar = "My static variable";

//declare a static function
MyClass.staticFunction = function ( pInput ) {
return new MyClass( MyClass.staticVar , pInput );
}

//create an instance
var oClass = new MyClass();

//run a static function (NO access to private or public)
oClass.staticFunction( 9 ); //staticFunction is undefined on an instance
//run a privileged member function on the class
MyClass.privilegedFunction(); //The function runs

Priveleged Member Variables & Functions

A privileged member function has access to private variables, but is available publicly.
function MyClass () {

//private member variable
var privateVar = "My Private Variable";

//privileged member function
this.privilegedFunction = function () {
alert( privateVar );
}
}
//create an instance
var oClass = new MyClass();

//run a privileged member function
oClass.privilegedFunction(); //Output: alerts the value of the private var

These classes may come as news to you…or maybe you’ve known about them since you were a fetus. In either circumstance, they’ve been a big help to me.

Word up.

Comments

7 Comments

  1. Daniel said over 2 years ago

    Awesome. I’ve been taking a bit of a look into JavaScript and this looks pretty darn nice. Keep it up bro!

  2. ed said over 2 years ago

    awethum.

  3. Walter said over 1 year ago

    Are there any books/sites you recommend for JavaScript?

  4. Gerard said over 1 year ago

    Thank you very much! You just showed me how to create private member variables and public static methods. Very nice!

  5. Christopher Hiller said over 1 year ago

    Excellent basic overview of this functionality.

  6. Dan said over 1 year ago

    Very nice, thanks for breaking it down barney style for us ;)

  7. Rogie said over 1 year ago

    @Dan – I’m glad you liked it man! Hope it helps.

Sorry, the comment form is closed at this time.