Log <-

Object Oriented Javascript

I never really gave it much thought, but it turns out that, naturally, Javascript supports Object Oriented programming. It's a bit awkward, but still:

/*****************************************************************
 * Class description
 *****************************************************************/
function MyClass(param1, param2) {
    /*------------------------------------------------------------
     * PROPERTIES
     *-----------------------------------------------------------*/
    this.property1 = false;
    thisObj.eventHappened = false;
 
    /*------------------------------------------------------------
     * METHODS
     *-----------------------------------------------------------*/
 
    /**
     * method1
     */
    this.method1 = function(param2) {
        this.property1 = param2;
    }
 
    /*------------------------------------------------------------
     * CONSTRUCTOR
     *-----------------------------------------------------------*/
    this.property2 = param1
    this.method1(param2)
 
    var out = "";
    out += "&lt;div id='"+param1+"'&gt;"+param2+"&lt;/div&gt;";
    document.write(out);
 
    this.div1 = document.getElementById(param1);
 
    /*------------------------------------------------------------
     * EVENTS
     *-----------------------------------------------------------*/
 
    /* Because 'this' in events is actually the event, we assign a
     * special variable here so we can reference this object from
     * the events.
     */
    var thisObj = this;
 
    this.div1.onkeyup = function(e) {
        alert("key");
        thisObj.eventHappened = true;
    }
}
 
var myClassInstance = MyClass('myDiv', 'Hello!');

Leave a Reply

You must be logged in to post a comment.