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

Javascript Callback/Object conflict

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.