views:

140

answers:

1

My Javascript knowledge is less experienced, so I might use wrong descriptions in the following.

I have an object in a static .js file:

var Info = {
    methodA: function() {
        // Call methodB.
        this.methodB('test');
    },

    methodB: function(value) {
        // Do stuff
    }
}

Now, in an .aspx file, I create a function methodC(value) with varying contents (depending on some data), which I want to insert instead of the above definition of methodB(value):

...
var methodC = function(value) {
    // Do different stuff
}
...

My idea has so far been to replace methodB with methodC in the following fashion:

...
Info.methodB = methodC;
...

Using IE's buildin developer tool, I get the following error when calling this.methodB('test'); from Info.methodA():

Object doesn’t support this property or method

Removing the 'this' from this.methodB('test') results in the error:

Object expected

I don't get any errors using FireBug - probably because I use various frameworks, which might catch the error.

How should I do this or should I use a completely different approach?

Regards, Casper

+3  A: 

It should work, you are doing it the right way. The problem lays elsewhere.

update: This should still work as long as you call methodA on an object, eg Info.methodA().

Maybe you are not understanding the error messages ?

"Object doesn’t support this property or method" means that in the expression "this.methodB()", this doesn't have a property named "methodB". So it means that this is not Info when the code of methodA is executed.

"Object expected" means that the variable methodB is unknown in the current execution context. Of course it is, since methodB is never a variable, only a property of Info.

To debug your problem, you need to know what is this when a code is executed, and why it's not what you think it should be. When you call Info.methodA(), this is set to be Info when methodA begins its execution.

Alsciende
Changing this.methodB('test') to directly call methodC('test') works - thus my implementation of methodC(value) should work. I know how to debug variables, but how to debug functions/methods, I haven't figured out yet :)
Chau
How can I see 'this' in IE's developer tool?
Chau
@Alsciende: In Firebug I can see the 'this' variable. But in IE's developer tool, I need to use the console and write something like: var a = this; Then 'this' is visible amongst the other local variables.
Chau