robnyman / domassistant

Automatically exported from code.google.com/p/domassistant
1 stars 0 forks source link

Use of foreach on Array in IE #6

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create an associative array in IE
2. for (var i in array) alert(i);
3. alert(i) prints DOMAssistant function names, then expected contents after.

What is the expected output? What do you see instead?
I use an Array to store the pages in my app, and in a foreach loop I loop
through the Array to manipulate the pages - show/hide.

What version of the product are you using? On what operating system?
2.6 on vista with IE7 (don't get this error on firefox)

Please provide any additional information below.
During the call to foreach the Array is found to contain all the function
names from DOMAssistant objects - cancelBubble, next,
replaceWithAJAXContent etc.

Here is my code
------
create Array by calling wsx.init with string array (each string is id of a
page in the app) - failure is on calling wsx.displayPage(pageId)
when it finds DOMAssistant function names and not page ids.

wsx.init(["cover_details", "customer", "eligibility", "declaration",
"direct_debit", "identifier"]);

=======
wsx object

var wsx = {
pages : null,
    init : function(somePages)
    {
        wsx.pages = new Array();
        for (var idx = 0; idx < somePages.length; idx++)
        {
            if (idx == 0)
            {
                wsx.pages[somePages[idx]] = { id : somePages[idx], next :
somePages[(1 + idx)], previous : "" };
                wsx.displayInlineDiv(somePages[idx]);
            }
            else if (idx == (1 + somePages.length))
            {
                wsx.pages[somePages[idx]] = { id : somePages[idx], next :
"", previous : somePages[(idx - 1)] };
                wsx.hideDiv(somePages[idx]);
            }
            else
            {
                wsx.pages[somePages[idx]] = { id : somePages[idx], next :
somePages[(1 + idx)], previous : somePages[(idx - 1)] };
                wsx.hideDiv(somePages[idx]);
            }
        }
    },

    displayPage : function(pageId)
    {
        for (var idx in wsx.pages)
        {
            var page = wsx.pages[idx].id;
            wsx.hideDiv(page);
        }
        wsx.displayInlineDiv(pageId);
        window.scrollTo(0, 0);
    }
}

Original issue reported on code.google.com by alexx...@gmail.com on 25 Mar 2008 at 5:30

GoogleCodeExporter commented 9 years ago
Hi Alex,

The way DOMAssistant is implemented, in IE specifically, is extending the native
Array object for performance and scalability reasons. This is because IE has a 
flawed
model when creating independent arrays inheriting from the native Array object.

We have been looking at other ways to approach this, but each solution does 
affect
performance in a negative way. If we find something good, we will implement it 
in the
next release.

When it comes to your code example, it is always best practice to check type in 
a
for...in loop. Doing this, you could then easily distinguish the type and get 
the
desired values.

You can read more about that in "for in Intrigue"
(http://yuiblog.com/blog/2006/09/26/for-in-intrigue/)

Best regards,
Robert

Original comment by robny...@gmail.com on 25 Mar 2008 at 3:45

GoogleCodeExporter commented 9 years ago
Hi Robert,

Thanks for your help, I'll make the for...in loop more robust.

Enjoying DOMAssistant - 80 lines of code just went to 3!

Best regards,
Alex

Original comment by alexx...@gmail.com on 25 Mar 2008 at 8:49

GoogleCodeExporter commented 9 years ago
That sounds great! I'm happy to hear that it helps out in your tasks! :-)

Best regards,
Robert

Original comment by robny...@gmail.com on 25 Mar 2008 at 9:41