473,378 Members | 1,492 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,378 software developers and data experts.

Javascript Callback/Object conflict

Hello- I have the following Javascript code

function Obj()
{
obj.squares = new Array();
}
Obj.prototype.Load= function(xdoc)
{
var goat = "head";
xdoc.ProcessNodes("squares/square", function(node)
{
alert(goat); // <-- this works fine
this.squares.push(
// do processing on node, create a square
);
});
}

Now the xdoc is another object I have, with a method called
ProcessNodes. The only important thing to know about ProcessNodes is
that it does some things, and calls its second argument (which is a
function) successively.

The problem I am running into here is once I get into the callback
function, "this.squares" is returning null ... even though it has been
initialized to an Array. For whatever reason, its like the object loses
scope when I get into the callback function. For all other intents and
purposes however, the callback function has the same scope as the
calling block (i.e. the goat variable is still accessible).

Any ideas? Seems like a bug to me.

Sep 2 '06 #1
1 2237
Hoss wrote:
function Obj()
{
obj.squares = new Array();
}
I assume that that was meant to be

this.squares = new Array(); // or: = [];

?
Obj.prototype.Load= function(xdoc)
{
var goat = "head";
xdoc.ProcessNodes("squares/square", function(node)
{
alert(goat); // <-- this works fine
this.squares.push(
// do processing on node, create a square
);
});
}
[snip]
The problem I am running into here is once I get into the callback
function, "this.squares" is returning null ... even though it has
been initialized to an Array.
No, the squares property of Obj "instance" is an array. However, the
this operator will not refer to that "instance" when ProcessNodes calls
your inner function.
For whatever reason, its like the object loses scope when I get into
the callback function.
It isn't about scope: the this operator value is determined by the way
in which a function is called. Scope only affects identifier resolution.
For all other intents and purposes however, the callback function has
the same scope as the calling block (i.e. the goat variable is still
accessible).
Yes. When that inner function is evaluated, its scope chain will contain
the variable object of the enclosing function. That variable object
contains all of the local variables, inner function declarations, and
formal arguments for the enclosing function as properties. Therefore,
attempting to resolve an identifier will involve checking those
properties if they are not hidden by variables within the function
expression.

Look at the FAQ notes and the Google archives for "closures".
Any ideas? Seems like a bug to me.
It is not. This has been discussed ad nauseum in the past and a search
of the Google archives for this group would have saved you quite some time.

<FAQENTRY>
As mentioned briefly above, the this operator value is determined by
/how/ a function is called. When called as a method, the operator will
refer to the object:

var MyObject = {
method : function() { return this.property; },
property : 'Some value'
};

MyObject.method(); // 'Some value'

However, calling the function directly will set the this operator value
to refer to the global object:

// continuing from previous...

var myFunction = MyObject.method;

myFunction(); // undefined; method is not global

Either pass a reference to the object to ProcessNodes, or create a
variable within the Load method that refers to the object and replace
the this operator with that variable.

Mike
If you reply, please remove the text in angle brackets (<FA**NTRY>).
Sep 3 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Guillaume CABANAC | last post by:
Hi everybody, Does anybody know how to access a remote database (say Oracle) from JavaScript code (within a Firefox Extension) ? I know ADO via ActiveX in the IE world and think a similar...
10
by: Chris Lieb | last post by:
Hi, I am working on an object that has a function that calls setInterval so that it can have a function called repeatedly. Right now, to call the function, you also have to provide the name of...
6
by: Howard | last post by:
I am having a problem that is driving me crazy and I am whits end. I am hoping that someone here can help me. I have a client server application where the client is drawing a progressive bar graph...
2
by: jobo | last post by:
I'm sure there's some way to do this simply but I'm new to Javascript. So I'm creating an application that interacts with google maps and everytime the user pans around the map I want it to send...
1
by: thomas_okken | last post by:
I'm working on a little C# DLL that will let me capture audio from within Internet Explorer. The idea is that the C# object, once it is recording, will periodically invoke a JavaScript callback,...
9
by: Meendar | last post by:
Hi, Below is my code snippet having only one form, <form> <input type ="radio" name="action" value="xyz" checked>xyz <input type ="radio" name="action" value="zyx">zyx <input type ="radio"...
1
by: Daniel Pitts | last post by:
I only need to support Firefox 3 at the moment, so any advice can be specific to that. I have a Java Applet, lets call it HelperApplet. It has one method of importance: public Object...
5
by: voidinutah | last post by:
Hello, I'm new to .NET and was trying to find a solution for having a button control do a post back then execute a javascript function. When the button is clicked a post back occurs to save...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.