Mootools when using For(…in Array) problem

后端 未结 3 1605
无人及你
无人及你 2021-01-19 07:56

This problem been there for couple years.

I am writing some plugins for a Forum engine called Discuz, I use a lot of Mootools for my own projects. When I plug Mootoo

相关标签:
3条回答
  • 2021-01-19 08:08

    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.

    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.)

    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.

    0 讨论(0)
  • 2021-01-19 08:20

    Mootools extends Array.prototype and maybe even Object.prototype, which can interfere with for .. in loops, which enumerate all properties, even those that exist in the object because they appear upwards on the prototype chain. Therefore, test if the property is a direct property of the object before using it:

    for(i in a)
        if (a.hasOwnProperty(i))
            console.log(i);
    
    0 讨论(0)
  • 2021-01-19 08:29

    Some newer browsers support ways to make all those extra properties (family, constructor, etc) be not enumerable, so they don't appear in the for-in loops.

    However, if you want to be able to support the old browsers as well I think you are out of luck: for-in is hardwired syntax so you wont be able to monkey-patch it. (Anyway, the magic find-replace seems to have worked...)

    0 讨论(0)
提交回复
热议问题