AS3 httpservice - 通过引用将参数传递给事件处理程序

发布于 2024-08-28 20:49:07 字数 737 浏览 8 评论 0原文

我有这样的代码:

var service:HTTPService = new HTTPService();
if (search.Location && search.Location.length > 0 && chkLocalSearch.selected) {
    service.url = 'http://ajax.googleapis.com/ajax/services/search/local';
    service.request.q = search.Keyword;
    service.request.near = search.Location;
} else
{
    service.url = 'http://ajax.googleapis.com/ajax/services/search/web';
    service.request.q = search.Keyword + " " + search.Location;
}
service.request.v = '1.0';
service.resultFormat = 'text';
service.addEventListener(ResultEvent.RESULT, onServerResponse);
service.send();

我想将搜索对象传递给结果方法(onServerResponse),但是如果我在闭包中执行此操作,它将按值传递。有没有一种方法可以通过引用来完成此操作,而无需在我的搜索对象数组中搜索结果中返回的值?

I have this code:

var service:HTTPService = new HTTPService();
if (search.Location && search.Location.length > 0 && chkLocalSearch.selected) {
    service.url = 'http://ajax.googleapis.com/ajax/services/search/local';
    service.request.q = search.Keyword;
    service.request.near = search.Location;
} else
{
    service.url = 'http://ajax.googleapis.com/ajax/services/search/web';
    service.request.q = search.Keyword + " " + search.Location;
}
service.request.v = '1.0';
service.resultFormat = 'text';
service.addEventListener(ResultEvent.RESULT, onServerResponse);
service.send();

I want to pass the search object to the result method (onServerResponse), but if I do it in a closure it gets passed by value. Is there a way to do it by reference without searching through my array of search objects for the value returned in the result?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栀子花开つ 2024-09-04 20:49:07

我不太确定你想在这里做什么。

参数确实是按值传递的。对于对象(这里的对象是指具有引用语义的所有内容,即除布尔值、数字、整数、字符串等之外的所有内容),对它们的引用是按值传递的,因此在您的函数中您有对原始对象,而不是对该对象副本的引用。

因此,如果您想取消引用该对象并更改某些值或对其调用某些方法,那就没问题了。唯一行不通的是改变引用本身;也就是说,您不能将其清空或为其分配一个新对象:

function dereferenceParam(param:Object):void {
    param.someInt = 4;
    param.someMethod();
}

function reassignParam(param:Object):void {
    param = null;
    //  or
    param = new Object(); 
}

dereferenceParam() 将按照大多数人的预期工作,而 reassignParam 则不会。

现在,我认为您根据最后一段可能遇到的唯一可能的“问题”是您想要从您拥有的某个数组中删除或清空搜索对象。恐怕在这种情况下,唯一的方法就是循环遍历数组。

I'm not quite sure what you want to do here.

Parameters are indeed passed by value. In the case of objects (and by object here I mean everything that has reference semantics, i.e. everything but Booleans, Number, ints, Strings, etc), a reference to them is passed by value, so in your function you have a reference to the original object, not a reference to a copy of the object.

So, if you want to dereference the object and change some value or call some method on it, you'll be ok. The only thing that wont work is changing the reference itself; i.e. you can't null it out or assign a new object to it:

function dereferenceParam(param:Object):void {
    param.someInt = 4;
    param.someMethod();
}

function reassignParam(param:Object):void {
    param = null;
    //  or
    param = new Object(); 
}

dereferenceParam() will work as most people expect, reassignParam won't.

Now, the only possible "problem" I think you could have as per your last paragraph would be that you want to remove or null out the search object from some array you have. I'm afraid in that case, the only way would be to loop through the array.

忆梦 2024-09-04 20:49:07

您如何确定您已收到该对象的副本?

据我所知,(非内在)对象几乎从不按值复制。唯一的例外是分派的 Event 对象,但这是明确记录的。

How are you determining that you have received a copy of the object?

To my knowledge, (non-intrinsic) objects are almost never copied by value. The only exception are dispatched Event objects, but that is explicitly documented.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文