1

我有这个代码:

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),但如果我在闭包中这样做,它会按值传递。有没有办法通过引用来做到这一点,而无需在我的搜索对象数组中搜索结果中返回的值?

4

2 回答 2

1

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.

于 2010-04-10T14:54:33.403 回答
0

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

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

于 2010-05-20T13:15:13.873 回答