关于Javascript类和执行的问题 [英] Question about Javascript classes and execution

查看:57
本文介绍了关于Javascript类和执行的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个小型的Ajax请求库,以简化我将在短期内完成的一些任务

。在大多数情况下一切正常工作

罚款,但是当我同时在

运行两个请求时,我似乎遇到了一些问题。第一个停止执行,第二个继续执行。

如果我在两个请求之间发出警报或者通过仅1毫秒的setTimeout运行第二个

,它们都是工作。您可以在这里查看一个工作示例: http: //www.lamatek.com/Ajax/test.html


我希望你们中的一些Javascript专家可以帮助我解释为什么这是

正在发生。


提前致谢。

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.

推荐答案

3月1点,1点33分,汤姆科尔 < tco ... @ gmail.comwrote:
On Mar 1, 1:33 pm, "Tom Cole" <tco...@gmail.comwrote:

我正在开发一个小的Ajax请求库来简化某些任务

我将很快接受。在大多数情况下一切正常工作

罚款,但是当我同时在

运行两个请求时,我似乎遇到了一些问题。第一个停止执行,第二个继续执行。

如果我在两个请求之间发出警报或者通过仅1毫秒的setTimeout运行第二个

,它们都是工作。您可以在这里查看一个工作示例: http: //www.lamatek.com/Ajax/test.html


我希望你们中的一些Javascript专家可以帮助我解释为什么这是

正在发生。
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.



你的代码是为了实现混乱行为而编写的,而且混乱的行为就是你所获得的行为。在示例页面中 - -r1 - 和 - r2

- 共享相同的函数对象 - doTextRequest - 方法,

且该方法只能引用当 - r2 - 被实例化时形成的

闭包的单个副本。


表面上你的问题是将属性对象分配给属性

AjaxRequest的原型。这些函数对象会导致创建

闭包,但只要创建了新的
AjaxRequest,并且所有预先存在的AjaxRequest实例都会更改正在使用的闭包

找到他们的方法附加到最后形成的闭包。


分享第一个闭包产生你描述的症状,因为

秒请求有效替换最后一个请求分配给 - xmlhttp -

的对象。混乱进来是因为如果你做了一个第一次请求,那么实例化一个新的AjaxRequest对象,然后做一个

的第二个请求(即使是通过任何预先存在的AjaxRequest对象)你

不会发生碰撞(因为它将在现在的

废弃关闭中运行),但是在完成
$ b $之前发出第三个请求b第二个,你回到了你开始的地方。因此,你有b / b $ b行为因你的代码使用方式而异(因此如果用
用户交互触发,行为取决于用户的_exact_sequence_

互动:混乱)。


Richard。

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.


Tom Cole写道:

嗨Tom,
Tom Cole wrote:

Hi Tom,

我正在开发一个小的Ajax请求库来简化某些任务

我会很快就要开始了。在大多数情况下一切正常工作

罚款,但是当我同时在

运行两个请求时,我似乎遇到了一些问题。第一个停止执行,第二个继续执行。
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.



这意味着这两个请求以某种方式发生冲突。快速浏览一下

你的图书馆暴露了设计中的一个根本缺陷,其中你的b $ b问题很可能是副作用。


您已将原型方法直接定义到构造函数中;这个

使它们成为最后一个创建实例的实例方法,也可以是以前实例可访问的
。因此,第一个查询的原型方法

可能会使用(某些)实例变量

第二个查询。


请考虑以下示例。


---

< script type =" text / javascript">

var Foo = function(bar){

Foo.prototype.getBar = function(){

返回栏;

}

}


var f1 = new Foo(1);

var f2 = new Foo(2);

alert(" f1.getBar()=" + f1.getBar()+" \ nf2.getBar()=" + f2.getBar());

< / script>

---


我想你想要一些先进的设计,正确定义

static /实例和私人/公共成员 - 这确实是通过这样的项目获得
的方式。请检查以下内容:


< URL:http://www.litotes.demon.co.uk/js_info/private_static.html>

<网址:http://www.jibbering.com/faq/faq_notes/closures.html>

亲切的问候,

Elegie。

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(bar){
Foo.prototype.getBar=function(){
return bar;
}
}

var f1=new Foo(1);
var f2=new Foo(2);
alert("f1.getBar()="+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.demon.co.uk/js_info/private_static.html>
<URL:http://www.jibbering.com/faq/faq_notes/closures.html>
Kind regards,
Elegie.


3月1日上午9:12,Richard Cornford < Rich ... @ litotes.demon.co.uk>

写道:
On Mar 1, 9:12 am, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:

3月1日下午1点33分, 汤姆科尔 < tco ... @ gmail.comwrote:
On Mar 1, 1:33 pm, "Tom Cole" <tco...@gmail.comwrote:

我正在开发一个小的Ajax请求库来简化某些任务

我将很快接受。在大多数情况下一切正常工作

罚款,但是当我同时在

运行两个请求时,我似乎遇到了一些问题。第一个停止执行,第二个继续执行。

如果我在两个请求之间发出警报或者通过仅1毫秒的setTimeout运行第二个

,它们都是工作。您可以在这里查看一个工作示例: http: //www.lamatek.com/Ajax/test.html


我希望你们中的一些Javascript专家可以帮助我解释为什么这是b / b
正在发生。
I''m hoping some of you Javascript experts can help me with why this is
happening.



你的代码是为了实现混乱的行为而编写的,而且混乱

的行为就是你得到的。在示例页面中 - -r1 - 和 - r2

- 共享相同的函数对象 - doTextRequest - 方法,

且该方法只能引用当 - r2 - 被实例化时形成的

闭包的单个副本。


表面上你的问题是将属性对象分配给属性

AjaxRequest的原型。这些函数对象会导致创建

闭包,但只要创建了新的
AjaxRequest,并且所有预先存在的AjaxRequest实例都会更改正在使用的闭包

找到他们的方法附加到最后形成的闭包。


分享第一个闭包产生你描述的症状,因为

秒请求有效替换最后一个请求分配给 - xmlhttp -

的对象。混乱进来是因为如果你做了一个第一次请求,那么实例化一个新的AjaxRequest对象,然后做一个

的第二个请求(即使是通过任何预先存在的AjaxRequest对象)你

不会发生碰撞(因为它将在现在的

废弃关闭中运行),但是在完成
$ b $之前发出第三个请求b第二个,你回到了你开始的地方。因此,你有b / b $ b行为因你的代码使用方式而异(因此如果用
用户交互触发,行为取决于用户的_exact_sequence_

互动:混乱)。


理查德。


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.



不是我理解你所说的一切,但这是我所关心的b $ b关注的一部分,我的方法并不是每个人独有的
AjaxRequest的实例。


了解测试页面上表示的行为类型是什么?b $ b我是什么的试图实现(能够创建多个AjaxRequest

对象并让它们的进程同时运行)我可以对AjaxRequest对象进行哪些更改?b $ b方法声明为

使它正常工作?

我是java开发人员,因此这种原型继承风格

我有点困惑。

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.


这篇关于关于Javascript类和执行的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆