Mootools在使用For(... in Array)问题时 [英] Mootools when using For(...in Array) problem

查看:73
本文介绍了Mootools在使用For(... in Array)问题时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题已经存在了几年了.

This problem been there for couple years.

我正在为称为Discuz的论坛引擎编写一些插件,我在自己的项目中使用了许多Mootools.当我将Mootools插入此论坛Engine(discuz)时,事情发疯了...

I am writing some plugins for a Forum engine called Discuz, I use a lot of Mootools for my own projects. When I plug Mootools into this forum Engine(discuz) things went crazy...

去年我做了一些调试之后,我发现问题是Mootools原型化了很多核心元素,包括Array,另一方面,这个论坛引擎使用了For(i in Array)Loop对于数组".肯定会引起问题,因为

After I did some debugging last year, I found that the problem is that Mootools prototype a lot of core elements, including Array, And, on the other hand, this forum engine uses a lof of For(i in Array) Loop for 'array'. Which will certainly cause problem because

for(i in [1,2,3,4,5]) console.log(i);  //0,1,2,3,4 the keys in this array 

**WITH MOOTOOLS
for(i in [1,2,3,4,5]) console.log(i);
//OUTPUT 0,1,2,3,4,$family,$constructor,pop,push,reverse,shift,sort,splice.......

上次我使用解析器更改所有for(i in array)循环以添加"if item.hasOwnProperty()"以传递这些原型 但是我认为这是一个非常糟糕的解决方法,因为您知道,会导致更多问题...新版本,修改其代码...等等

Last time i use a parser to change all for(i in array) loop to add an 'if item.hasOwnProperty()' to by pass those prototypes But i think this is a very bad work-around cause u know, cause more problems...new versions, bug up their codes...etc

我想知道是否有解决此问题的方法?无需触摸此论坛引擎的任何js代码,也可以使用Mootools?

I wonder if there is work around to slove this problem? without touching any of this forum engine's js code, and also use Mootools?

我知道对数组使用For(.. in)是不好的,但是我的问题是我不想碰这个论坛引擎的javascript代码,我只想找到一个解决问题的解决方案

推荐答案

您应使用提到的hasOwnProperty().我不确定您为什么会认为这会引起更多问题,实际上,对于for(x in y)循环,我倾向于默认使用hasOwnProperty(),而仅在特殊情况下才忽略它.

You should use hasOwnProperty() as you mentioned. I'm not sure why you think this would cause more problems, indeed with a for(x in y) loop I'd be inclined to use hasOwnProperty() by default and only omit it for special cases.

话虽如此,我不会在数组上使用for(x in y)循环.通常最好使用标准的for(i=0; i<y.length; i++)循环,该循环当然会忽略所有非数字属性. (除非也许您知道您有非连续的数组索引,否则在这种情况下for(x in y)会跳过未使用的索引,但是即使在那种情况下,我可能仍会使用标准的for并测试undefined在循环中.)

Having said that, I wouldn't use a for(x in y) loop on an array. It's generally better to use a standard for(i=0; i<y.length; i++) loop which will of course ignore all the non-numeric properties. (Except perhaps if you know you've got non-consecutive array indexes, in which case for(x in y) will skip the unused indexes, but even in that case I'd probably still use a standard for and test for undefined within the loop.)

更新:好的,我现在知道了.如果您想继续使用MooTools,插入hasOwnProperty()的解决方法是我能想到的最佳解决方案.在for)结束位置之后,应该合理可以安全地插入它:您无需检查现有的{}括号或添加自己的括号.

UPDATE: OK, I get it now. Your work-around to insert the hasOwnProperty() is the best solution I can think of if you want to keep using MooTools. Should be reasonably safe to insert it just after the closing ) of the for: you shouldn't need to check for existing {} brackets or add your own.

这篇关于Mootools在使用For(... in Array)问题时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆