Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
43
rated 0 times [  47] [ 4]  / answers: 1 / hits: 42057  / 15 Years ago, tue, july 7, 2009, 12:00:00

I am having a JavaScript namespace say



A={

CA: function() {
this.B();
},
B: function() {
var test='test';
var result='t1';

C: function() {
this.test='test1';
.....
.....
return 'test1';
}

result=this.C();
return result;
}
}


Now when I am executing such code it is giving that TypeError: this.C is not a function. Can somebody tell me why it is so. I know it is something related with lexical scoping but am unable to understand this.


More From » methods

 Answers
246

I think the problem is that when this.C() is executed inside the function referred to by B, this refers to the object that contains B, that is, object A. (This assumes B() is called within the context of A)



The problem is, C does not exist on the object A, since it's defined within B. If you want to call a local function C() within B, just use C().



EDIT:
Also, I'm not sure what you've posted is valid JavaScript. Specifically, B should be defined this way, since you can't use the object:property syntax within a function.



B: function()
{
var test='test';
var result='t1';

var C = function()
{
this.test='test1';
return 'test1';
}

result=C();
return result;
}

[#99178] Thursday, July 2, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
damiondenzelc

Total Points: 268
Total Questions: 116
Total Answers: 116

Location: North Korea
Member since Tue, Jun 16, 2020
4 Years ago
;