Javascript inheritance: is my solution correct? I have been developing Javascript for few months, and, as a former Java developer, needed a simple way to perform class inheritance.

My needs are:


having private members not accessible (encapsulation),
to make inheritance simple,
to avoid 'new' keywords bugs,
to let the superclass methods accessible using the this.super property.


The purpose of this request is to indicate me if the solution I developped should be applied into my project or if there are some misbehaviour risks by using it, and if you have remarks on how to enhance it.

My code is below, thank you for your reviews.

/**
* Allows to retrieve the global object (used to test if the 'new' keyword is needed)
*/
function getGlobal(){
    return (function(){
            return this;
    }).call(null);
}

if (typeof Object.extend !== 'function') {
    Object.extend = function (object, superClassConstructor, superConstructorArgs) {
        //Inherits from the super object
        superClassConstructor.apply(object, superConstructorArgs);

        //make this.super allow to access to inherited object original functions
        object.super = {};
        for (var property in object){
            object.super[property] = object[property];
        }
    };
}
//The superclass
function SuperClass(options){
    //Make sure we will not alter global object
    if (this === getGlobal()){ 
        throw new Error("Constructor cannot be called as a function (new keyword should not be omitted)");
    }
    //Use of jshashtable-2.1:
    var attributes = new Hashtable();

    for (var key in options){
        attributes.put(key, options[key]);
    }

    this.setValue = function(key, value){
        if(value != null){
            attributes.put(key, value);
        }
        else{
            attributes.remove(key);
        }
    };

    this.getValue = function(key){
        return attributes.get(key);
    };
}

/**
* Inheriting class
*/
function ProjectModel(projectId) {
    //Make sure we will not alter global object
    if (this === getGlobal()){ 
        throw new Error("Constructor cannot be called as a function (new keyword should not be omitted)");
    }
    //extends SuperClass
    Object.extend(this, SuperClass, [{
                                    id:projectId,
                                    'one':1,
                                    'two':"2"
                                    }]);

    this.myType = "ProjectNumber";
    //Overriding a function
    this.getValue = function(key){
        return this.myType + ":" + this.super.getValue(key);
    };
}

var project = new ProjectModel(123);
alert(project.getValue('one')); //this displays 'ProjectNumber:123'
It's generally a bad idea to try writing code in one language as if it were a different language. 

Don't fight your tool; use it as it was designed. JavaScript has prototype-based OO rather than class-based OO, so use it like that.

Read "Javascript - the Good Parts" and learn to write actually good JavaScript rather than "Java in JavaScript" (for which other developers working with your code will hate and/or mock you).