473,765 Members | 2,047 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about Javascript classes and execution

I'm working on a small Ajax request library to simplify some tasks
that I will be taking on shortly. For the most part everything works
fine, however I seem to have some issues when running two requests at
the same time. The first one stops execution as the second continues.
If I place either an alert between the two requests or run the second
through a setTimeout of only 1 millisecond, they both work. You can
see a working example here: http://www.lamatek.com/Ajax/test.html.

I'm hoping some of you Javascript experts can help me with why this is
happening.

Thanks in advance.

Mar 1 '07 #1
18 1929
On Mar 1, 1:33 pm, "Tom Cole" <tco...@gmail.c omwrote:
I'm working on a small Ajax request library to simplify some tasks
that I will be taking on shortly. For the most part everything works
fine, however I seem to have some issues when running two requests at
the same time. The first one stops execution as the second continues.
If I place either an alert between the two requests or run the second
through a setTimeout of only 1 millisecond, they both work. You can
see a working example here:http://www.lamatek.com/Ajax/test.html.

I'm hoping some of you Javascript experts can help me with why this is
happening.
Your code is written to achieve chaotic behaviour, and chaotic
behaviour is what you get. In the example page both of - r1 - and - r2
- share the same function object as their - doTextRequest - method,
and that method only has the ability to reference a single copy of the
closure formed when - r2 - is instantiated.

Superficially your problem is assigning function objects to properties
of the prototype of AjaxRequest. These function objects cause a
closure to be created, but the closure in use changes whenever a new
AjaxRequest is created, and all pre-existing AjaxRequest instances
find their methods attached to the last closure formed.

Sharing that 1st closure produces the symptoms you describe, as the
second request effectively replaces the object assigned to - xmlhttp -
with the last request made. The chaos comes in because if you made a
first request, instantiated a new AjaxRequest object and then made a
second request (even through any pre-existing AjaxRequest object) you
would not get the collision (as it would be operating in a now
abandoned closure), but a third request made before the completion of
the second and your are back to where you started. Thus you have
behaviour varying with how your code is used (and so if triggered by
user interaction, behaviour depending on the _exact_sequence _ of user
interactions: chaos).

Richard.

Mar 1 '07 #2
Tom Cole wrote:

Hi Tom,
I'm working on a small Ajax request library to simplify some tasks
that I will be taking on shortly. For the most part everything works
fine, however I seem to have some issues when running two requests at
the same time. The first one stops execution as the second continues.
This means that the two requests are somehow colliding. A quick look at
your library exposed a fundamental flaw in the design, of which your
problem is likely to be a side effect.

You have defined the prototype methods right into the constructor; this
makes them instance methods of the last created instance, also
accessible to previous instances. As a result, your prototyped methods
for the first query probably use (some of) the instance variables of
your second query.

Consider the following example.

---
<script type="text/javascript">
var Foo=function(ba r){
Foo.prototype.g etBar=function( ){
return bar;
}
}

var f1=new Foo(1);
var f2=new Foo(2);
alert("f1.getBa r()="+f1.getBar ()+"\nf2.getBar ()="+f2.getBar( ));
</script>
---

I suppose you wanted to have some advanced design, properly defining
static/instance and private/public members - which is indeed the way to
go with such project. Check the following:

<URL:http://www.litotes.dem on.co.uk/js_info/private_static. html>
<URL:http://www.jibbering.c om/faq/faq_notes/closures.html>
Kind regards,
Elegie.
Mar 1 '07 #3
On Mar 1, 9:12 am, "Richard Cornford" <Rich...@litote s.demon.co.uk>
wrote:
On Mar 1, 1:33 pm, "Tom Cole" <tco...@gmail.c omwrote:
I'm working on a small Ajax request library to simplify some tasks
that I will be taking on shortly. For the most part everything works
fine, however I seem to have some issues when running two requests at
the same time. The first one stops execution as the second continues.
If I place either an alert between the two requests or run the second
through a setTimeout of only 1 millisecond, they both work. You can
see a working example here:http://www.lamatek.com/Ajax/test.html.
I'm hoping some of you Javascript experts can help me with why this is
happening.

Your code is written to achieve chaotic behaviour, and chaotic
behaviour is what you get. In the example page both of - r1 - and - r2
- share the same function object as their - doTextRequest - method,
and that method only has the ability to reference a single copy of the
closure formed when - r2 - is instantiated.

Superficially your problem is assigning function objects to properties
of the prototype of AjaxRequest. These function objects cause a
closure to be created, but the closure in use changes whenever a new
AjaxRequest is created, and all pre-existing AjaxRequest instances
find their methods attached to the last closure formed.

Sharing that 1st closure produces the symptoms you describe, as the
second request effectively replaces the object assigned to - xmlhttp -
with the last request made. The chaos comes in because if you made a
first request, instantiated a new AjaxRequest object and then made a
second request (even through any pre-existing AjaxRequest object) you
would not get the collision (as it would be operating in a now
abandoned closure), but a third request made before the completion of
the second and your are back to where you started. Thus you have
behaviour varying with how your code is used (and so if triggered by
user interaction, behaviour depending on the _exact_sequence _ of user
interactions: chaos).

Richard.
Not that I understand everything you said, but this is part of what my
concern was, that my methods were not unique to each instance of an
AjaxRequest.

Understanding that the type of behaviour denoted on the test page is
what I'm trying to achieve (being able to create multiple AjaxRequest
objects and have their processes run at the same time) what changes
can I make to the AjaxRequest object and it's method declarations to
make it work properly?

I'm a java developer and therefore this prototype style of inheritance
confuses me a bit.

Mar 1 '07 #4
On Mar 1, 9:22 am, Elegie <ele...@invalid .comwrote:
Tom Cole wrote:

Hi Tom,
I'm working on a small Ajax request library to simplify some tasks
that I will be taking on shortly. For the most part everything works
fine, however I seem to have some issues when running two requests at
the same time. The first one stops execution as the second continues.

This means that the two requests are somehow colliding. A quick look at
your library exposed a fundamental flaw in the design, of which your
problem is likely to be a side effect.

You have defined the prototype methods right into the constructor; this
makes them instance methods of the last created instance, also
accessible to previous instances. As a result, your prototyped methods
for the first query probably use (some of) the instance variables of
your second query.

Consider the following example.

---
<script type="text/javascript">
var Foo=function(ba r){
Foo.prototype.g etBar=function( ){
return bar;
}

}

var f1=new Foo(1);
var f2=new Foo(2);
alert("f1.getBa r()="+f1.getBar ()+"\nf2.getBar ()="+f2.getBar( ));
</script>
---

I suppose you wanted to have some advanced design, properly defining
static/instance and private/public members - which is indeed the way to
go with such project. Check the following:

<URL:http://www.litotes.dem on.co.uk/js_info/private_static. html>
<URL:http://www.jibbering.c om/faq/faq_notes/closures.html>

Kind regards,
Elegie.
Ahhh, "I see" said the blind man. I'm starting to get it. I noticed
that if I either:

1. Replace 'AjaxRequest.pr ototype' with 'this'
or
2. Move the prototype methods outside the AjaxRequest function

I get more like what I expected.

Thank you very much. Maybe someday I'll actually be able to code a
decent Javascript block. :)

Mar 1 '07 #5
On Mar 1, 2:25 pm, Tom Cole wrote:
>On Mar 1, 1:33 pm, Tom Cole wrote:
<snip>
Not that I understand everything you said,
Some of it was maybe not that clearly expressed.
but this is part of what my concern was, that my
methods were not unique to each instance of an
AjaxRequest.
But mostly you do not want methods to be unique to each instance of a
javascript object. The only methods (or, the only function objects
acting as methods) that you want to be unique are the ones that
absolutely must be unique to achieve the desired goal.
Understanding that the type of behaviour denoted on the test
page is what I'm trying to achieve (being able to create
multiple AjaxRequest objects and have their processes run at
the same time) what changes can I make to the AjaxRequest
object and it's method declarations to make it work properly?
What are you asking for? A course on the more advanced aspects of
javascript code design or me re-writing your code for you?
I'm a java developer and therefore this prototype style of
inheritance confuses me a bit.
You haven't got to inheritance yet, you are still apparently
misconceiving the role of prototypes in defining objects.

Richard.

Mar 1 '07 #6
VK
On Mar 1, 4:33 pm, "Tom Cole" <tco...@gmail.c omwrote:
I'm working on a small Ajax request library to simplify some tasks
that I will be taking on shortly. For the most part everything works
fine, however I seem to have some issues when running two requests at
the same time. The first one stops execution as the second continues.
If I place either an alert between the two requests or run the second
through a setTimeout of only 1 millisecond, they both work. You can
see a working example here:http://www.lamatek.com/Ajax/test.html.
Richard Cornford and Elegie already hinted you to the base of the
problem. I have - possibly bad - habit to try to explain some
javascript issues to Java/C++ people by using classy terms. Surely it
may be considered as getting a foreign joke from a word-by-word ruby
translation :-) but it also may work better than some abstract
explanations.

AjaxRequest.pro totype.doTextRe quest = function(){//...
};

is round around as

public class AjaxRequest {
public static void doTextRequest() {
// ...
}
}

here is where you are getting problems with two concurrent calls. Also
please note that in initiateRequest your variable "type" is declared
without var so created as global variable and not as local withing the
function scope. It may be what you wanted - I did not study the whole
code - but it's worth to mention.

For a good sample of AJAX library with multi-threaded request and
request grouping capabilities I suggest to take a look at AjaxRequest
<http://www.ajaxtoolbox .com>

Mar 1 '07 #7
On Mar 1, 5:14 pm, VK wrote:
On Mar 1, 4:33 pm, "Tom Cole" <tco...@gmail.c omwrote:
<snip>
Richard Cornford and Elegie already hinted you to the base of the
problem. I have - possibly bad - habit to try to explain some
javascript issues to Java/C++ people by using classy terms.
Your bad habit is trying to impress people who don't know any better.
As to "classy terms", you have made it abundantly clear in the past
that you have no understanding of class-based languages and the
meaning of the terminology used to talk about them.
Surely it may be considered as getting a foreign joke from a
word-by-word ruby translation :-) but it also may work better
than some abstract explanations.
Making misleading suggestions to people will nerve "work better" when
the desire is to promote understanding.
AjaxRequest.pro totype.doTextRe quest = function(){//...

};

is round around as

public class AjaxRequest {
public static void doTextRequest() {
// ...
}
Assigning function objects to the properties of a constructor's
prototype is conceptually equivalent to defining an instance method in
class-based languages (the - this - keyword - in such methods is
expected to be employed to refer to the object instance).
here is where you are getting problems with two concurrent calls.
As the master of chaotic script authoring you are unlikely to
understand, but the problem has nothing to do with your notion that
the methods assigned as properties of a prototype must be 'static'
because there is only one function object representing each. In the
code each invocation of the constructor creates a unique set of
function objects for each object instance; it just puts them where
they will be shared by all existing instances (and denies access to
the previous set at the same time).
Also please note that in initiateRequest your variable "type" is
declared without var so created as global variable and not as local
withing the function scope.
So you did not notice, or did not recognise, the closure formed with
the constructor, and its declaration of - var type = "text"; -?
It may be what you wanted - I did not study the whole
code - but it's worth to mention.
You could not have understood the code even if you had studied it.
For a good sample of AJAX library with multi-threaded request and
request grouping capabilities I suggest to take a look at AjaxRequest
<snip>

It is a better example, but still not good (particularly in its
needless use of excessive inner functions).

Richard.
Mar 1 '07 #8
VK
On Mar 1, 8:46 pm, "Richard Cornford" <Rich...@litote s.demon.co.uk>
Your bad habit is trying to impress people who don't know any better.
As to "classy terms", you have made it abundantly clear in the past
that you have no understanding of class-based languages and the
meaning of the terminology used to talk about them.
Yes, I know your personal position that JavaScript is so utterly
complicated and foreign to any other language that any explanations
can be given only by Books of ECMA and God forbid to make any
analogies with any other language. Let's say I listened your
argumentation in full but prefer to remain in my own opinion.

In the posed question let's also keep two defferent things separated:
prototype inheritance and scope manipulations using closures. In my
"prototype ~ static" analogy I was talking about the prototype
inheritance alone. And the prototype inheritance would be more
correctly called "prototype borrowing" because instances to not get
any ownership of methods in the prototype chain. But at the moment of
the creation they get informed where to "borrow" the needed method for
the execution time. [this] inside of the shared method is being set to
the "borrower" for the time of the execution making possible this type
of inheritance - indeed fancy thought - to work. This way
MyObject.protot ype.myMethod = function(){}
is kind of an "automated" way of

function foo() {
}

function MyObject() {
this.myMethod = foo;
}

In either case we are getting a single method allocation being used by
all instances. This is why - with all important and not so important
differences - static method in Java is the closest equivalent.

Mar 1 '07 #9
On Mar 1, 6:22 pm, "VK" <schools_r...@y ahoo.comwrote:
On Mar 1, 8:46 pm, "Richard Cornford" <Rich...@litote s.demon.co.uk>
>Your bad habit is trying to impress people who don't know any
better. As to "classy terms", you have made it abundantly
clear in the past that you have no understanding of
class-based languages and the meaning of the terminology
used to talk about them.

Yes, I know your personal position that JavaScript is so utterly
complicated and foreign to any other language that any explanations
can be given only by Books of ECMA and God forbid to make any
analogies with any other language.
Nonsense, I have a long record of supporting the use of terminology
from class-based languages when talking about concepts that are
analogous in javascript.
Let's say I listened your argumentation in full but prefer
to remain in my own opinion.
Let's say you do not understand what I am talking about and rather
than admitting that you would prefer to ignore me.
In the posed question let's also keep two defferent things
separated: prototype inheritance and scope manipulations
using closures. In my "prototype ~ static" analogy I was
talking about the prototype inheritance alone.
And that is where you are wrong. The appropriate concept for the act
of assigning a reference to a function object to a property of a
constructor's prototype is the defining of an instance method. All
objects created with the constructor inherit that method, and in that
method the - this - keyword can be used to refer to the object
instances. The methods are "of the class instances" not "of the
class", and so are not analogous to "static" in Java.
And the prototype inheritance would be more
correctly called "prototype borrowing" because instances
to not get any ownership of methods in the prototype chain.
Twit.
But at the moment of
the creation they get informed where to "borrow" the needed method for
the execution time. [this] inside of the shared method is being set to
the "borrower" for the time of the execution making possible this type
of inheritance - indeed fancy thought - to work. This way
MyObject.protot ype.myMethod = function(){}
is kind of an "automated" way of

function foo() {
}

function MyObject() {
this.myMethod = foo;
}

In either case we are getting a single method allocation being
used by all instances.
Having a single function object instance shared between all instances
of a 'class' is not at all a bad thing. It is certainly much more
efficient that creating a new function object to act as a method for
each instance of a class (leaving that second approach to only be
employed when the unique identity of such function objects brings with
it direct benefits).

The number of function objects being employed has little relevant to
the applicability of 'static' to instance method definitions in
javascript. In Java there is no concrete manifestation of a method;
they are not objects in their own right. But the probability is that
each and every instance of a class is executing a single copy of the
byte-code that represents one of its instance methods (or the compiled
to machine code equivalent). So were the nearest manifestation of a
method in Java is the bytes of executable code in a computer's memory
the fact that all instances of a class will share that same code can
be regarded as exactly as significant as all instances of a javascript
'class' sharing a single function object.
This is why - with all important and not so important
differences - static method in Java is the closest equivalent.
The javascript structures that are analogous to static methods in Java
are methods of the constructor and closure-contained functions that
are accessible to all instances of a class but inaccessible to any
other code (public static and private static respectively). Such
constructs are very obviously "of the class", indeed the methods of
the constructor refer to the constructor function object with the -
this - keyword. Trying to attach the term "static" to any other
structures (particularly instance methods) is only going to confuse.

Richard.

Mar 1 '07 #10

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

Similar topics

38
5089
by: Shaun McKinnon | last post by:
HI...Here's my problem...I have a popup window that loads when i want it to, but it's not sized properly. I've set the size, but it doesn't seem to work. I've been on 8 different websites to find out what i'm doing wrong, and so far it seems i'm doing it the right way. Here's my code...any suggestions would be appreciated. <script language="javascript"> <!-- window.open("256fx/index.htm", "", "height=400, width=600"); //-->
6
2535
by: Andy Fish | last post by:
Hi, I want to use an anchor tag to invoke some javascript and I've read that it's bad form to use <a href="javascript:foo()"> I've read endless usenet posts and hint sites on the net, they all suggest different things and I can't get any kind of consistency, and I can't find any solution that works properly for IE, opera and mozilla. many of the recommended solutions go something like this:
18
1676
by: dover | last post by:
Instantiation of classes is to create objects in areas of memory. As a side effect of that object creation, the constructor is called. Three steps can be identified in an instantiation of a class: memory allocation, object creation, and call constructor. My understanding is the call of constructor that triggers memory allocation and object creation, though I understand when in constructor, the memory has been allocated and object...
3
1535
by: Gopinath | last post by:
Hi JavaScript Gurus, I've a question on Regular Expressions using RegExp object. I just want to know whether it is possible to do the search (see below) using RegExp. Any pointers would be of immense help. Thanks. My simple JavaScript code (inside a function): ======================= var strList = "~@!~1#Apple~@!~2#Orange~@!~3#Mango~@!~4#Grapes~@!~";
10
3482
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read somewhere that each folder under the "web site" is compiled in separate assembly. I however, did not find that the "web site" creation in vs.net 2005 created any AssemblyInfo.cs file.
15
1925
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following two? (1)
21
6055
by: petermichaux | last post by:
Hi, I've been asking questions about library design over the last week and would like to get feedback on my overall idea for a JavaScript GUI library. I need a nice GUI library so there is a good chance I will write this as I need new widgets. I haven't found anything like this and I'm surprised/disapointed this doesn't already exist. My library prototype works nicely. I think parts of these ideas are not commonly used for JavaScript...
7
1661
by: db | last post by:
Hi@all Just got a comparison problem with javascript. I want to compare : document.getElementById(currentID).getAttribute("style") with a string, e.g: "background-color: lightgreen;" They are exactly the same, but the execution result seems they are not equal. It works with firefox, but not with IE.
11
1964
by: emailscotta | last post by:
Below I declared a basic object literal with 2 methods. The "doSomething" method is call from the "useDoSomething" method but the call is only sucessful if I use the "this" keyword or qualify the call with "SomeObj". Can someone describe why this is happening? var SomeObj = { doSomething : function() {
0
9568
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
10007
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...
1
9957
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9835
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
8832
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...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
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();...
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.