andreasblixt
August 12th, 2007, 05:30 AM
Hi there, I'd like some feedback on my take on creating classes and inheritance in JavaScript.
I considered prototype inheritance (ChildClass.prototype = new BaseClass(); ...), but didn't like the idea of creating an instance of a class to inherit from -- I would either need code in the constructor to exit it if it was being used for inheritance or risk the constructor doing things outside its scope (affecting other objects, creating XmlHttpRequests, ...). I didn't like either of the options and went with copying all the members instead.
The base() method is basically direct access to the constructor of the parent class.
Here's the code:
Object.extend = function(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
};
var Class = {
create: function (members, inherits) {
var newClass = function () {
this.construct.apply(this, arguments);
};
if (inherits) {
Object.extend(newClass.prototype, inherits.prototype);
newClass.prototype.base = function () {
inherits.prototype.construct.apply(this, arguments);
};
}
Object.extend(newClass.prototype, members);
return newClass;
}
};
var Greeter = Class.create({
name: "",
construct: function (name) {
this.name = name;
},
hello: function () {
alert("Hello " + this.name + "!");
}
});
var GreeterDeluxe = Class.create({
construct: function (name, surName) {
this.base(name);
this.name += " " + surName;
},
bye: function () {
alert("Bye " + this.name + "!");
}
}, Greeter);
var a = new GreeterDeluxe("Andreas", "Blixt");
a.hello(); // "Hello Andreas Blixt!"
a.bye(); // "Bye Andreas Blixt!"
I considered prototype inheritance (ChildClass.prototype = new BaseClass(); ...), but didn't like the idea of creating an instance of a class to inherit from -- I would either need code in the constructor to exit it if it was being used for inheritance or risk the constructor doing things outside its scope (affecting other objects, creating XmlHttpRequests, ...). I didn't like either of the options and went with copying all the members instead.
The base() method is basically direct access to the constructor of the parent class.
Here's the code:
Object.extend = function(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
};
var Class = {
create: function (members, inherits) {
var newClass = function () {
this.construct.apply(this, arguments);
};
if (inherits) {
Object.extend(newClass.prototype, inherits.prototype);
newClass.prototype.base = function () {
inherits.prototype.construct.apply(this, arguments);
};
}
Object.extend(newClass.prototype, members);
return newClass;
}
};
var Greeter = Class.create({
name: "",
construct: function (name) {
this.name = name;
},
hello: function () {
alert("Hello " + this.name + "!");
}
});
var GreeterDeluxe = Class.create({
construct: function (name, surName) {
this.base(name);
this.name += " " + surName;
},
bye: function () {
alert("Bye " + this.name + "!");
}
}, Greeter);
var a = new GreeterDeluxe("Andreas", "Blixt");
a.hello(); // "Hello Andreas Blixt!"
a.bye(); // "Bye Andreas Blixt!"