JavaScript Class Definitions

May 24, 2007

I had a hard time understanding basic classes in JavaScript and the tutorials I found all got way to bogged down in useless fluff, so I thought I’d put up this no-nonsense example of a JavaScript class. Expect a version using Base.js once I learn that way.

function myClass () {
  var privateVariable = 'Private';
  this.publicVariable = 'Public';

  this.getPrivate =
    function () {
      return privateVariable;
    }
}

myCopy = new myClass();
alert(myCopy.publicVariable);
alert(myCopy.getPrivate());
alert(myCopy.privateVariable);

Output (In Alerts)
Public
Private
undefined

Categories: Geek
Tags: , ,

Leave A Comment

Your email will not be published.