解决方案 »

  1.   

    到现在我都没有在javascript用过enumeration,看过enumeration。
    在javascript中这些新奇玩意,看不到源代码,还真的不知所云。不过我想用绝对会用,因为在java中看多了用多了。
      

  2.   

    var obj = {first : "prop1", second: "prop2", 3: "prop3"};var s = "";
    for (var key in obj) {
        s += key + ": " + obj[key] + " ";
    }
    document.write (s);
    Running this sample in Internet Explorer 8 and Internet Explorer 9 would print the following results:
    All modes of Internet Explorer 8:
    first: prop1 second: prop2 3: prop3
    All modes of Internet Explorer 9:
    3: prop3 first: prop1 second: prop2
    Internet Explorer 8 does not include enumerations of properties that have the same name as built-in properties of a prototype object. All document modes in Internet Explorer 9 include these properties in the enumeration. The following sample illustrates this difference:var obj = { first: "prop1", toString : "Hello" }
    var s = "";
    for (var key in obj) {
        s += key + ": " + obj[key] + " ";
    }
    document.write (s);Running this sample in Internet Explorer 8and Internet Explorer 9 would print the following results:
    All modes of Internet Explorer 8:
    first: prop1
    All modes of Internet Explorer 9:
    first: prop1 toString: Hello
      

  3.   

    Thank you for your participation. Here, the enumeration I mean differs from what you presented. The enumeration I mean is a data type like which in C/C++/Java. JavaScript reserved a key word enum for further improvement but has no implementation of built-in enumeration type. The issue you pointed out deserves noting though. In many versions of IE, the for/in loop won't enumerate an enumerable property of an object if the property of the object has a nonenumerable property by the same name. Pay attention to the word ed red. It has to be a nonenumerable property of the prototype with the same name, and it doesn't need to be a built-in property( user-defined property also complies).
    To work around the bug you mentioned, we can code the following to take care of the two possibilities:
    for(var p in {toString: null}) {
       // If we get here, then the for/in loop works correctly, nothing bothers
       ......
    }
    // If we get here, it means that the for/in loop did not enumerate the toString property of the test object.
    // We need to take care of some properties that may not be enumerated ourselves.
       .......