jcouyang / closure-library

Automatically exported from code.google.com/p/closure-library
0 stars 0 forks source link

Not safe to use object.clone in ie8 when using object properties #530

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago

What steps will reproduce the problem?
1. Run the following code in ie8

        var a = [1,2,3,4];
    var b = goog.object.clone(a);

    var i=0;
    for(var key in a){
        if (!a.hasOwnProperty(key))
            continue;
        i++;
    }
    alert("Normal array has " + i + " properties.");

    i=0;
    for(var key in b){
        if (!b.hasOwnProperty(key))
            continue;
        i++;
    }
    alert("Cloned array has " + i + " properties.");

Unsure if a fix actually can exist for IE8, posting here encase there's an off 
chance it might. 

Work around if it's an array is to never use 'for(var key in obj)', always  use 
'for(var i = 0; i < obj.length; i++)'.

Original issue reported on code.google.com by ali.taf...@googlemail.com on 17 Jan 2013 at 12:14

GoogleCodeExporter commented 9 years ago
We explicitly disallow iterating over arrays with for-in in our JavaScript 
style guide:

http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml?showone=fo
r-in_loop#for-in_loop

Original comment by mart...@google.com on 20 Feb 2013 at 12:34