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 に答える