Have a look at this snipped:
There is a transformator with a transformMap()-method. This method calls two other methods of the transformator: transformMapHead() and transformMapBody()
transformator
var transformator =
{
transformMap: function(tree) {
return transformator.transformMapHead(tree)
+transformator.transformMapBody(tree.body[0].body) +"}\n";
},
transformMapHead: function(tree) {
return "to be done";
},
transformMapBody: function(tree) {
// completly implemented
},
...
};
Ok, fine so far. The transformMapBody()-method is completly implemented, the transformMapHead()-method isn't, this will be done in a child-object.
Now, lets have a look at a more concrete transformator. Here I only want to implement the transformMapHead()-method.
But, when I implement the
concreteTransformator
var concreteTransformator = Object.beget(transformator);
concreteTransformator.transformMapHead = function(tree)
{
// my business logic
console.log("i am the new business logic");
};
it doesn't work correctly. Instead of running the transformMapHead()-method from the concreteTransformator-object, the method from the first transformator-object is executed and "to be done" ist printed out.
I can avoid this by copying the transformMap()-method from the transformer-object to the concreteTransformer-object:
concreteTransformator.transformMap = function(tree) {
return concreteTransformator.transformMapHead(tree)
+concreteTransformator.transformMapBody(tree.body[0].body) +"}\n";
};
Then, everything runs fine, but this cannot be the way to go?!
At last, the beget-helper-function from Douglas Crockford:
if (typeof Object.beget !== 'function') {
Object.beget = function(o) {
var F = function() {};
F.prototype = o;
return new F();
};
}