本文介绍了用于AJAX封装的OO Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读''Ajax in Action'',(许多科技书籍中最好的一本

我已读过)并且有一个例子我只是不完全明白。

如果你碰巧拿到了这本书,那就是第75页。我希望有人可以给b
。 br />

var loader = this;

this.req.onreadystatechange = function(){

loader.onReadyState.call(loader) ;

}


我的问题是:为什么装载机被分配了这个值?为什么不

只是用这个?我假设JS中的对象是按值传递的,所以对象的副本是

,但我还是不完全理解

上下文是如何变化的。


提前致谢。


拉里

I''m reading ''Ajax in Action'', (one of the best of the many tech books
I''ve read) and there''s an example that I just don''t fully understand.
If you happen to have the book, it''s on page 75. I''m hoping someone can
enlighten me.

var loader = this;
this.req.onreadystatechange = function() {
loader.onReadyState.call(loader);
}

My question is this: Why is loader assigned a value of this? Why not
just use this? I''m assuming objects in JS are passed by value so a
copy of the object is made, but I still don''t fully understand how the
context changes.

Thanks in advance.

Larry

推荐答案



啊,关闭。 ..


好​​吧,这可能是

JavaScript中最难理解的概念之一,所以请耐心等待。


这个想法是这样的:事件处理程序只能分配一个无参数的

函数。在onreadystatechange的情况下,分配的函数是一个

anonymous,无参数函数。


但是,访问参数会很好,例如(在那个

的情况下)封装XmlHttpRequest的对象。但是,当执行

函数时,上下文已经改变,因为它是一个全局的

函数(它没有分配给对象(这不会)工作),在全局对象的上下文中执行
。在Web浏览器中,

全局对象是窗口。当执行该函数时,this ;

指的是窗口,就像一个简单的测试所示。


值得庆幸的是,JavaScript已经关闭。这意味着,当一个函数是

定义,它的(外部)上下文也被保存,并且可以从函数内部访问
。函数外部定义的变量

将是可见的并且在调用函数时可以访问,无论

函数的上下文是什么!


那么代码的作用是:保存包含在this在

a变量外部的函数(loader),然后定义匿名,

参数-f f当函数被调用时,由于

闭包,外部变量将是可访问的。


小心使用闭包,因为它可以是内存泄漏的来源

(通过循环引用),特别是在AJAX应用程序中,

页面不经常重新加载。


这有意义吗?


HTH,

Laurent

-

Laurent Bugnion ,GalaSoft

软件工程:

PhotoAlbum:

支持加尔各答的儿童:

Ah, closure...

OK, it''s probably one of the most difficult concepts to understand in
JavaScript, so bear with me.

The idea is this: An event handler can only be assigned a parameter-less
function. In the case of onreadystatechange, the function assigned is an
anonymous, parameter-less function.

However, it would be nice to access parameters, for example (in that
case) the object encapsulating the XmlHttpRequest. But, when the
function is executed, the context has changed, because it''s a global
function (it is not assigned to the object (that wouldn''t work), it is
executed in the context of the global object. In web browsers, the
global object is the window. When the function is executed, "this"
refers to the window, as a simple test would show.

Thankfully, JavaScript has closure. It means that, when a function is
defined, its (external) context is also saved, and will be accessible
from inside the function. Variables defined externally to the function
will be visible and accessible when the function is called, whatever the
function''s context is!

So what the code does here is: Save the reference contained in "this" in
a variable external to the function (loader), then define the anonymous,
parameter-less function. When the function will be called, thanks to
closure, the external variable will be accessible.

Use closure with care, because it can be the source of memory leaks
(through circular references), especially in AJAX applications, where
the page is not reloaded very often.

Does that make sense?

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch





也许最好使用常规术语函数引用:

事件处理程序只能分配一个函数引用或

表达式返回函数引用。

无参数函数"有点不清楚:有人可以把它当作

事件处理程序/匿名函数不能有参数(虽然它们可以是
)。


之后,这是对clj发布的此次尿失禁现象的最佳描述

现象相当一段时间。

P.S. < OT>

在这方面JavaScript中的私人会员包含一个声明

这不完全正确:

function foo(){} // 1)



var foo = function(){}; // 2)

不相等2)只是一个< q>快捷形式< / qof 1)

在第一种情况下,我们声明命名函数" ; FOO" (一个记忆

项目)。

在第二种情况下,我们创建变量foo并赋予它

右手表达的结果。这个结果是对匿名函数的引用(两个内存项:我们的匿名函数和

foo引用它)。

大多数时候它并不重要,但有时候知道很有用。

< OT>

Maybe it would be better to use the regular term "function reference":
"An event handler can only be assigned a function reference or an
expression returning function reference".
"parameter-less function" is a bit unclear: someone can take it as if
event handler / anonymous functions cannot have arguments (while they
can).

After that it''s the best description of the "incontinence of [this]"
phenomenon posted at c.l.j. for a rather while.
P.S. <OT>
In this aspect "Private Members in JavaScript" contains one statement
which is not totally correct:
function foo() {} // 1)
and
var foo = function() {}; // 2)
are not equal there 2) is just a <q>shortcut form</qof 1)
In the first case we are declaring named function "foo" (one memory
item).
In the second case we are creating variable "foo" and assigning to it
the result of the right-hand expression. This result is a reference to
the anonymous function (two memory items: our anonymous function and
"foo" holding a reference to it).
Most of the times it is not important, but sometimes useful to know.
<OT>


这篇关于用于AJAX封装的OO Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 05:10