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

Async XMLHttpRequest and class variables

P: n/a
Hi,
I'm currently building a parser class in JS and I have a question
about variables. I retrieve XML data and then process it. After that, I
process a result array (mydataarray). It looks like this :

function MyClass()
{

var mydataarray = [];

this.processXMLnode = processXMLnode;
this.processxmlDoc = processxmlDoc;
this.processrequest = processrequest;

function processXMLnode(marker) {
// ADD SOMETHING TO mydataarray
}

function processxmlDoc(xmlDoc) {
var markers =
xmlDoc.documentElement.getElementsByTagName("data" );
mydataarray = [];

for (var i = 0; i < markers.length; i++) {
processXMLnode(markers[i]);
}

// PROCESS mydataarray
}

function processrequest(url, async) {
var request = XMLHttpRequest.create();

request.open("GET", url, async);

if (async) {
request.onreadystatechange = function() {
if (request.readyState == 4) {
processxmlDoc(request.responseXML);
}
}
request.send(null);
}
else
{
request.send(null);
processxmlDoc(request.responseXML);
}
}

}

I'm not a JS expert, does processxmlDoc will be executed from the same
thread (or execution context) every time? Does processxmlDoc is called
only when nothing else is executing? Otherwise, how can I use a safe
array?

Cheers
Gabriel

Dec 13 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
ga*************@gmail.com wrote:
I'm currently building a parser class in JS
The below is not a class declaration, instead a function declaration
and probably a constructor. You are not using any of the class-based
OOP features of the latest JavaScript/JScript/ECMAScript versions/editions,
instead you are using inner functions and probably the prototype-based
OOP features (with `new FunctionObjectReference()').
and I have a question about variables. I retrieve XML data and then
process it. After that, I process a result array (mydataarray). It
looks like this :

function MyClass()
{
[...]

function processxmlDoc(xmlDoc) {
var markers =
xmlDoc.documentElement.getElementsByTagName("data" );
mydataarray = [];

for (var i = 0; i < markers.length; i++) {
processXMLnode(markers[i]);
}

// PROCESS mydataarray
}

function processrequest(url, async) {
var request = XMLHttpRequest.create();

request.open("GET", url, async);

if (async) {
request.onreadystatechange = function() {
if (request.readyState == 4) {
processxmlDoc(request.responseXML);
}
}
request.send(null);
}
else
{
request.send(null);
processxmlDoc(request.responseXML);
}
}

}

I'm not a JS expert, does processxmlDoc will be executed from the
same thread (or execution context) every time?
No. The use of `async' here implies a value that can be type-converted
to boolean. Therefore, if the value of `async' is a true-value, the
event listener is assigned. It it, processxmlDoc() is called iff
request.readyState equals 4 (success) when the `readystatechange' event
occurs. If, on the other hand, the value of `async' is a false-value,
processxmlDoc() is always called [after request.send(null)].
Does processxmlDoc is called only when nothing else is executing?
Yes, the respective programming languages are single-threaded.
The inevitable delay in processing is mitigated, but not completely
compensated by the use of event handlers for host objects.
Otherwise, how can I use a safe array?


It is already safe as it is.
PointedEars
Dec 13 '05 #2

P: n/a
I understand what I've done, hopefully ;) In fact I've created that
pseudo class to access inner functions to be able to do something like
:

function a() {
b();
}

function b() {
c();
}

function c() {
a();
}

With some "if" of course ;)

Understood for the single thread context.
Cheers!

Thomas 'PointedEars' Lahn wrote:
ga*************@gmail.com wrote:
I'm currently building a parser class in JS


The below is not a class declaration, instead a function declaration
and probably a constructor. You are not using any of the class-based
OOP features of the latest JavaScript/JScript/ECMAScript versions/editions,
instead you are using inner functions and probably the prototype-based
OOP features (with `new FunctionObjectReference()').
and I have a question about variables. I retrieve XML data and then
process it. After that, I process a result array (mydataarray). It
looks like this :

function MyClass()
{
[...]

function processxmlDoc(xmlDoc) {
var markers =
xmlDoc.documentElement.getElementsByTagName("data" );
mydataarray = [];

for (var i = 0; i < markers.length; i++) {
processXMLnode(markers[i]);
}

// PROCESS mydataarray
}

function processrequest(url, async) {
var request = XMLHttpRequest.create();

request.open("GET", url, async);

if (async) {
request.onreadystatechange = function() {
if (request.readyState == 4) {
processxmlDoc(request.responseXML);
}
}
request.send(null);
}
else
{
request.send(null);
processxmlDoc(request.responseXML);
}
}

}

I'm not a JS expert, does processxmlDoc will be executed from the
same thread (or execution context) every time?


No. The use of `async' here implies a value that can be type-converted
to boolean. Therefore, if the value of `async' is a true-value, the
event listener is assigned. It it, processxmlDoc() is called iff
request.readyState equals 4 (success) when the `readystatechange' event
occurs. If, on the other hand, the value of `async' is a false-value,
processxmlDoc() is always called [after request.send(null)].
Does processxmlDoc is called only when nothing else is executing?


Yes, the respective programming languages are single-threaded.
The inevitable delay in processing is mitigated, but not completely
compensated by the use of event handlers for host objects.
Otherwise, how can I use a safe array?


It is already safe as it is.
PointedEars


Dec 13 '05 #3

P: n/a
ga*************@gmail.com wrote:
I understand what I've done, hopefully ;) In fact I've created that
pseudo class to access inner functions to be able to do something like
:

function a() {
b();
}

function b() {
c();
}

function c() {
a();
}

With some "if" of course ;)
Now that is real nonsense.

Say I call a(), it calls b(), which calls c(), which calls a(), ...
Say I call b(), it calls c(), which calls a(), which calls b(), ...
Say I call c(), it calls a(), which calls b(), which calls c(), ...

And there is no inner function whatsoever.

If the above is instead but a bad example and you assumed that you
would be able to call b.c() as in

function b()
{
function c()
{
// ...
}
}

then this is not entirely true. What would be used then is a _JavaScript_
(Mozilla/2+) extension to ECMAScript, it is not available in JScript (IE)
or Opera, for example.

One of many correct ways to implement a public method is

function Foo()
{
this.bar = function()
{
// ...
}
}

var a = new Foo();
a.bar();

See
<URL:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide#Details_of_the_Object_Mo del>
Understood for the single thread context.
At least.
[Full quote]


Do not top-post on Usenet.

<URL:http://jibbering.com/faq/faq_notes/pots1.html#ps1Post>
PointedEars
Dec 14 '05 #4

P: n/a
Thomas 'PointedEars' Lahn wrote:
If [...] you assumed that you would be able to call b.c() as in

function b()
{
function c()
{
// ...
}
}

then this is not entirely true. What would be used then is a _JavaScript_
(Mozilla/2+) extension to ECMAScript, it is not available in JScript (IE)
or Opera, for example.


Example: function x() { function y() { alert("42"); } }; x.y() // 42 or not

It turns out that this feature is no longer available in JavaScript 1.6
as implemented in Firefox 1.5 (Mozilla/5.0 rv:1.8), while it still is in
JavaScript 1.5 as implemented in Mozilla/5.0 rv:1.7.12. The Support
Matrix[1] is growing larger every day :)
PointedEars
___________
[1] <URL:http://PointedEars.de/scripts/js-version-info>
Dec 14 '05 #5

P: n/a
Thomas 'PointedEars' Lahn wrote:
ga*************@gmail.com wrote:
I understand what I've done, hopefully ;) In fact I've created that
pseudo class to access inner functions to be able to do something like
:

function a() {
b();
}

function b() {
c();
}

function c() {
a();
}

With some "if" of course ;)
Now that is real nonsense.


Ok my scheme was really simplified here, should I put :

function doSomething() {
processrequest('backtrack.php?button='+this.someth ing, true);
}

function processXMLnode(marker) {
element = createNewButton(marker);
element.onclick = doSomething();
}

function processxmlDoc(xmlDoc) {
var markers =
xmlDoc.documentElement.getElementsByTagName("data" );
mydataarray = [];

for (var i = 0; i < markers.length; i++) {
processXMLnode(markers[i]);
}
}

function processrequest(url, async) {
var request = XMLHttpRequest.create();

request.open("GET", url, async);

if (async) {
request.onreadystatechange = function() {
if (request.readyState == 4) {
processxmlDoc(request.responseXML);
}
}
request.send(null);
}
else
{
request.send(null);
processxmlDoc(request.responseXML);
}
}

See
<URL:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide#Details_of_the_Object_Mo del>

Thanks. I still believe that "prototype" thingy is oversized for my
problem.
Do not top-post on Usenet.

<URL:http://jibbering.com/faq/faq_notes/pots1.html#ps1Post>

Sorry about that!

My very first draft is there :
http://gabriel.landais.org/testGM/map.inc.js . It doesn't work very
well with IE :) Thanks to your site, I hope I will be able to
understand why!

Cheers

Dec 14 '05 #6

P: n/a
ga*************@gmail.com wrote:
Ok my scheme was really simplified here, should I put :

[...]
Looks OK, but there are no inner functions whatsoever left :)

Or did you mean that you used that inside a function in order
to avoid spoiling the global namespace? Then I'll agree.
See
<URL:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide#Details_of_the_Object_Mo del>


Thanks. I still believe that "prototype" thingy is oversized for my
problem.


If you create only one object with calling new MapClass(), then yes.
If not, you should consider the prototype since inheriting it from the
prototype as in

function Map()
{
// ..
}

Map.prototype.processXMLnode = function()
{
// ...
}

prevents each Map object to have a method (Function object) of its own
(initially) which will save you heap; on the other hand, it removes the
possible advantage of closures to define methods specific to an object
on initialization.
My very first draft is there :
http://gabriel.landais.org/testGM/map.inc.js .
Ahh, that approach is OK then. A bit old-fashioned IMHO, as function
expressions allow for

this.geturlproperties = function()
{
// ...
}

instead of

this.geturlproperties = geturlproperties;

function geturlproperties()
{
// ...
}

since JavaScript 1.3/JScript 2.0?/ECMAScript 3.
It doesn't work very well with IE :) Thanks to your site, I hope I
will be able to understand why!


Hmmm ... getAttribute() is often buggy and may not be necessary.

For responseXML to work in IE (with MSXML), you have to make sure
that the response is served at least with Content-Type: text/xml.

<URL:http://msdn.microsoft.com/library/en-us/xmlsdk/html/ab1b76cf-7dd9-46b5-b782-cc04823df117.asp>

Apart from that, I do not see syntax or semantical errors, but
I may have overlooked some, especially those that may be in the
constructors you call that are not defined in map.inc.js.
HTH

PointedEars
Dec 14 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.