By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,627 Members | 1,188 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,627 IT Pros & Developers. It's quick & easy.

Javascript object scope reference

P: n/a
Hello All,

I am a newbie and trying to understand the variable scopes in JS. I
will try to explain my problem here and see if I make sense.

I created an object test.pgObject=function()
{
.........
this.param=param;
this.runOptions() {
this.buildList();
.......
}
this.buildList(){
this.param;
........
}
}

now i include this object js file in my other pages and initialize
object myobj=new test.pgObject();

then on same page, I have a <Atag which onClick calls
"myobj.runOptions();" . It finds myobj.runoptions fine and object
reference is correct, however when it calls this.buildList , reference
of 'this' changes to window object instead myobj. then i get into
error because it does not find object with 'this' reference.

Is there anyways to do it correctly so that i don't run into problem
like this?

thanks
Jan 18 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Jan 18, 11:27*am, Rails junkie <ABoxForTheOtherSt...@gmail.com>
wrote:
Hello All,

I am a newbie and trying to understand the variable scopes in JS. I
will try to explain my problem here and see if I make sense.

I created an object *test.pgObject=function()
{
* .........
* this.param=param;
* this.runOptions() {
* this.buildList();
......}

this.buildList(){
this.param;
.......

}
}
Post real code.
>
now i include this object js file in my other pages and initialize
object myobj=new test.pgObject();

then on same page, I have a <Atag which onClick calls
"myobj.runOptions();" . It finds myobj.runoptions fine and object
reference is correct, however when it calls this.buildList , reference
of 'this' changes to window object instead myobj. then i get into
The "this" identifier has nothing to do with scope.
Jan 18 '08 #2

P: n/a
Yeah, post real code, what you've said is happening doesn't make a
great deal of sense in relation to the code you've posted. Perhaps the
problem is the scope of the call, do a search for function.apply if
nothing else.

L

On Jan 19, 3:27 am, Rails junkie <ABoxForTheOtherSt...@gmail.com>
wrote:
Hello All,

I am a newbie and trying to understand the variable scopes in JS. I
will try to explain my problem here and see if I make sense.

I created an object test.pgObject=function()
{
.........
this.param=param;
this.runOptions() {
this.buildList();
......}

this.buildList(){
this.param;
.......

}
}

now i include this object js file in my other pages and initialize
object myobj=new test.pgObject();

then on same page, I have a <Atag which onClick calls
"myobj.runOptions();" . It finds myobj.runoptions fine and object
reference is correct, however when it calls this.buildList , reference
of 'this' changes to window object instead myobj. then i get into
error because it does not find object with 'this' reference.

Is there anyways to do it correctly so that i don't run into problem
like this?

thanks
Jan 18 '08 #3

P: n/a
It's worth noting that David's solution is the more correct of the
two.

On Jan 19, 8:56 am, Rails junkie <ABoxForTheOtherSt...@gmail.com>
wrote:
Thank you both. I am going to take my time and understand the solution
presented by you guys.
I will let you know if this work.
Jan 18 '08 #4

P: n/a
then on same page, I have a <Atag which onClick calls
"myobj.runOptions();" . It finds myobj.runoptions fine and object
reference is correct, however when it calls this.buildList , reference
of 'this' changes to window object instead myobj. then i get into
error because it does not find object with 'this' reference.
Basically, whenever you use browser to open an HTML file even an blank
one, it automatically creates an object called 'window' which is the
most top Object. If you didn't indicate any object id/name and tried
to call a method, the JavaScript thinks that you're calling method in
'window' object instead. But it seemed that you didn't try to define
any method for 'window' object, and that's why error appeared. Hope
this helps, also.

from http://njoscript.blogspot.com/
Jan 19 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.