0

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();
};

}

1 Answer 1

1

The problem is that you are referencing a specific variable inside transformMap (transformator), which contains a reference to the original object. To understand the implications, consider what would happen if you did:

var foo = transformator;
transformator = null;
foo.transformMap();

You would get an error because transformator is null now and therefore does not have a property transformMapHead anymore.


You should be using this instead:

return this.transformMapHead(tree)
       +this.transformMapBody(tree.body[0].body) +"}\n";

to refer to the current object.

Learn more about this.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Felix, thanks for your answer. I tried 'this' before. Using "this" results in a TypeError: Object #<Object> has no method 'transformMapHead'
How are you calling transformMap? It must be concreteTransformator.transformMap(). There are other ways to explicitly set what this should refer to inside a function, but we would have to know how you call the function in order to give a proper suggestion.
Mea Culpa! The problem was in the calling-statement, inside the nodejs-exports-beget-aliencode :) Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.