patternjavascriptModerate
Javascript inheritance: is my solution correct?
Viewed 0 times
javascriptsolutioninheritancecorrect
Problem
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:
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
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
Solution
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).
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).
Context
StackExchange Code Review Q#5806, answer score: 15
Revisions (0)
No revisions yet.