473,320 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Javascript object scope reference

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
4 2992
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Andy Fish | last post by:
Hi, can anybody put forward a sensible argument javascript's behaviour of creating a new global variable whenever I assign to a previously undeclared variable. I can't beleive this is just for...
72
by: Stephen Poley | last post by:
I have quite often (as have probably many of you) come across HTML forms with irritating bits of Javascript attached. The last straw on this particular camel's back was a large form I was asked to...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
6
by: ged | last post by:
Hi, i am a oo (c#) programmer, and have not used javascript for a while and i cant work out how javascript manages its references. Object References work for simple stuff, but once i have an...
2
by: mr.mattyg | last post by:
I might as well start off like everyone else who posts problems they are having....So I'm new to JavaScript..... Anywho, I have a page that lists 15 or so thumbnails and then one big image of...
20
by: shapper | last post by:
Hello, How to create a namespace in Javascript containing two methods? And how to access those methods? Thanks, Miguel
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.