473,809 Members | 2,469 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=f unction()
{
.........
this.param=para m;
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.runOptio ns();" . It finds myobj.runoption s 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 3021
On Jan 18, 11:27*am, Rails junkie <ABoxForTheOthe rSt...@gmail.co m>
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=para m;
* 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.runOptio ns();" . It finds myobj.runoption s 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 <ABoxForTheOthe rSt...@gmail.co m>
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=f unction()
{
.........
this.param=para m;
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.runOptio ns();" . It finds myobj.runoption s 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 <ABoxForTheOthe rSt...@gmail.co m>
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.runOptio ns();" . It finds myobj.runoption s 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
6725
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 the sake of convenience (surely we learned this much from basic). here's my proposal: all "global" (document scope) variables must be declared by 'var' outside a function block.
72
5249
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 complete in connection with attendance at a seminar. After spending more than 15 minutes on it, I clicked on the submit button - and nothing happened. Looking round the pages on Javascript form validation that Google produced for me (well,...
136
9468
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 code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to use. The above URL is version 1.0 (draft) that resulted. IMO, it is not a replacement for the FAQ,...
6
5586
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 object collection and stanrd using it it starts to fall apart. Clearly there is something about javascript's usage of passing "By ref" that i am not getting. i have had a look on the web and found some examples, but i cant see why my code does not...
2
6200
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 one of those thumbnails. I wrote some javascript code that when you click on the thumbnail the real picture of that thumbnail loads in the big image on the page. So essentially I wrote some very simple code that replaces the "src" of an image tag...
20
2308
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
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10643
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10378
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10121
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.