1

我从 Google Map API 获得了一个用于调整高度“地图”的示例代码

 var recalcHeight = function() {
    $("#map").height($(window).height() - 100); //Example
    map && google.maps.event.trigger(map, 'resize'); //(*) Don't understand this line
}; 
$(window).resize(recalcHeight);
recalcHeight();

好的,在我的 php 文件中,我有一个 div 标签: 1. 我不明白上面的 (*) 行。

4

1 回答 1

2

I don't really know what you question is about because you didn't tag it with Javascript, but I think if you knew this, you would have asked what google.maps.event.trigger(X, Y) did. Plus, I don't know what this has to do with your div tags in your PHP files.

Google Maps API answer

It's simple to find out what trigger does here:

Triggers the given event. All arguments after eventName are passed as arguments to the listeners.

(From the Google Maps API, I found it in no time)

Javascript answer

If your question is about this whole && construct, I'll explain it to you:

map && google.maps.event.trigger(map, 'resize');

The && is a logical AND with short-cirquit evaluation which means:

  • If its first argument is true, it evaluates the second argument. Iff this is true too, it returns true.

  • If its first argument is false, the whole expression can't become true, so it doesn't even evaluate its second argument and returns false immediately.

This mechanism can be (ab)used to change flow control. If the condition (&&'s first argument) is false, the second argument doesn't get evaluated, so it's the same as

if (map) {
    google.maps.event.trigger(map, 'resize');
}

I don't know how map was initialized, but maybe the event trigger is added only if there's a map element.

于 2012-09-23T12:31:34.297 回答