Mootools 毁掉了我的全局变量!!!谷歌地图 +穆工具
我使用 Mootools 来做一些事情,比如凹凸盒和 Google 地图应用程序中的东西。一切都 100% 工作,在我添加 Mootools 脚本之前,无论我在声明全局变量之前还是之后添加脚本,Mootools 都会杀死它们...看一下片段
<script language="javascript" type="text/javascript" src="mootools.js"></script>
<script language="javascript" type="text/javascript" src="bumpbox.js"></script>
<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript">
</script>
<script type="text/javascript">
var currentBounds;
var currentMarker = null;
var currentInfo = null;
var markersArrayProps = [];
var markersArrayAreas = [];
</script>
还有更多...但这只是显示了正在发生的事情,如果我删除调用 mootools.js
的行,一切都会正常。基本上,在 XMLHTTPRequest 期间,markersArrayProps
的填充方式如下:
markersArrayAreas.push(marker);
然后,有一个用于idle
的地图的事件侦听器,它会执行一些计算以查看是否应该删除区域标记,并且根据地图的边界加载属性标记。无需了解细节,但每当调用此 idle
函数时,我都会像这样循环遍历数组:
function clearAreaOverlays() {
if (markersArrayAreas) {
for (i in markersArrayAreas) {
markersArrayAreas[i].setMap(null);
}
}
}
它返回 markersArrayAreas[i].setMap()
是不是一个函数。仅当页面中包含 mootools 脚本时,如果我删除 mootools,一切正常。有什么想法吗?
Im using Mootools for a few things like bumpboxes and stuff in a Google Maps application. Everything was working 100%, before I added the Mootools script, it doesn't matter if I add the script before or after I declare my globals, Mootools kills them...take a look at a snippet
<script language="javascript" type="text/javascript" src="mootools.js"></script>
<script language="javascript" type="text/javascript" src="bumpbox.js"></script>
<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript">
</script>
<script type="text/javascript">
var currentBounds;
var currentMarker = null;
var currentInfo = null;
var markersArrayProps = [];
var markersArrayAreas = [];
</script>
There's a lot more...but this just shows whats going on, everything works fine if I remove the line calling mootools.js
. Basically markersArrayProps
is populated like this during an XMLHTTPRequest:
markersArrayAreas.push(marker);
Then there is a event listener for the map being idle
which does some calculations to see if it should drop the area markers, and load property markers based upon the bounds of the map. No need to get into the details, but whenever this idle
function is called, and I loop through the array like this:
function clearAreaOverlays() {
if (markersArrayAreas) {
for (i in markersArrayAreas) {
markersArrayAreas[i].setMap(null);
}
}
}
It returns markersArrayAreas[i].setMap()
is not a function. Only with the mootools script included in the page, if I remove mootools, everything works. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解决此问题的最简单方法是使用以下模式重写循环:
看起来您正在使用markerArrayAreas 作为标准整数索引数组。因此,您不应使用
(for i in array)
模式来迭代它。首先,它比使用基于索引的计数循环效率低得多。但此外,此模式会迭代对象的所有成员。就您而言,MooTools 使用一些更多的自定义函数扩展了数组原型,因此每当您创建数组时,除了编号的数组值之外,对象中现在还有其他属性。如果打印
i
的值,您应该会发现它不再只是数字。您可以通过使用
hasOwnProperty
函数检查每个属性名称是否引用未从原型继承的属性来解决此问题,但同样,这将是效率比低得多。有关显示这些不同循环结构的相对性能的快速基准测试,请参见此处:http://jsperf.com /用于测试
The easiest way to fix this is to rewrite your loop using the pattern:
It looks like you're using markersArrayAreas as a standard integer-indexed array. So, you shouldn't use the
(for i in array)
pattern to iterate through it. First of all, it's much less efficient than using an index-based counting loop.But furthermore, this pattern iterates through all members of an object. In your case, MooTools has extended the Array prototype with some more custom functions, so whenever you create an array, there are now additional properties in the object besides the numbered array values. If you print the value of
i
you should see that it's no longer just numbers.You could get around this by checking to see if each property name refers to a property that wasn't inherited from the prototype using the
hasOwnProperty
function, but again, this would be much less efficient than.For a quick benchmark that shows the relative performance of these different looping constructs, see here: http://jsperf.com/for-in-test
好的,感谢大家的意见,特别感谢 Jason 指出 mootools 数组原型......使用 mootools
.each
函数Ok, thanks for everyones input, and a special thanks to Jason for pointing out about the mootools array prototype....using mootools
.each
function您可以命名这些全局变量。这需要一些返工,但可以保证您的内容免受其他脚本的影响:
然后,每当您引用这些变量时,只需进入该对象即可:
You could namespace those global variables. It would require a bit of reworking, but it's guaranteed to keep your stuff safe from other scripts:
Then, whenever you refer to those variables, just go into the object: