Javascript
En Javascript vamos a implementar una clase Punto3D que hereda de la clase Punto implementada antes aquí.
// Implementamos una función para herencia
function inherit(superClass, subClass) {
for(var i in superClass.prototype) {
subClass.prototype[i] = superClass.prototype[i]
}
}
Test.StandardPoint3D = function (x, y, z) {
this.x = x || 0
this.y = y || 0
this.z = z || 0
}
// Hacemos que Test.Standard sea la super clase de Test.StandardPoint3D
inherit(Test.StandardPoint, Test.StandardPoint3D)
// No podemos asignar un nuevo prototipo porque ya tenemos uno de la super clase
Test.StandardPoint3D.prototype.getZ = function () {
return this.z
}
Test.StandardPoint3D.prototype.setZ = function (z) {
this.z = z;
}
var superMethod = Test.StandardPoint3D.prototype.clear;
Test.StandardPoint3D.prototype.clear = function () {
superMethod.apply(this);
this.z = 0;
}
Joose
Ahora reimplementamos la clase Punto3D usando la librería Joose y vemos que el nuevo código es más compacto y legible porque se ocupa de lo que nos importa.
Module("Test", function (m) {
Class("Point3D", {
isa: m.Point,
has: {
z: {
is: "rw",
init: 0
}
},
after: {
clear: function () {
this.setZ(0)
}
}
})
})