楼主:
alair99 (I think home)
2014-08-25 17:08:04程式码请参考 http://jsbin.com/sirotikujicu/1/edit?html,output
这个程式主要是用来让使用者点选google map上的marker之后,
跳出一个infowindow,然后显示其文字。
其中有一段
google.maps.event.addListener(marker,'click', function(content){
return function() {
infowindow.setContent(content);//set the content
infowindow.open(map,this);
}
}(content));
请问这一段第2行为什么要用return function ,跟第4行的this有关吗?
礼拜六参考google大神写的,
但是现在竟然看不懂了 = =|||
作者:
s25g5d4 (function(){})()
2014-08-26 00:05:00是野生的闭包没错, 因为事件触发一定在 for 循环结束后若没有用闭包包起来 那么 content 永远是循环最后一次的资料, 用闭包包起来才可以保存 content 不被盖掉至于那个 this 跟闭包无关, 就只是 handler 执行时可能用了 call 或 apply 把 this 改成 marker稍微修改一下就能验证 this === marker