1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

javascript BUG: delete before for in statements breakes prototype.

Discussion in 'Internet Explorer' started by _JM_, Feb 3, 2010.

  1. _JM_

    _JM_ Flightless Bird

    I wrote a piece of code that shows the problem.
    When you delete a member of an object with a delete operator, you should be
    able to see the prototype's value of the same property name if it exists
    (because of chaining).

    This works fine unless you try to do a for in statement of the object after
    the delete, in which case the value of the prototype is not returned, in fact
    it's never retrieved at all.

    The example has two instances of Bar, both of them should have 3 members
    before and after the delete, but after the delete only 2 members are found on
    one of the instances.

    This very example works fine in all other browsers (Firefox, Chrome, Opera).
    If you do almost anything with the bar variable after the delete an before
    the for in (for example uncomment the comparation bar==0) then the for in
    statement works fine, just like it should always work.

    This bug could produce some very serious problems on code that would seem to
    work under most common cirumstances.

    Here's the code:

    str="";
    var display = function(nombre, obj)
    {
    var i = 0;
    str+:-("<br/>"+nombre+"<br/>");
    for (var a in obj){
    str+:-("&nbsp&nbsp "+a + "="+ obj[a]+"<br/>");
    i++;
    }
    str+:-("<br/>"+i+"<br/>");
    }

    var Bar = function()
    {
    this.instMember = "bar";
    }

    Bar.prototype.name = "proto";
    Bar.prototype.id = 3;

    bar = new Bar();
    bar2 = new Bar();
    bar.name = "inst";

    //display objects before delete
    display("bar", bar);
    display("bar2", bar2);

    str+:-("<br/>----- delete bar.name<br/>");
    delete bar.name;
    //bar==0;

    //display objects after delete
    display("bar", bar);
    display("bar2", bar2);

    document.write(str);


    ----------------
    This post is a suggestion for Microsoft, and Microsoft responds to the
    suggestions with the most votes. To vote for this suggestion, click the "I
    Agree" button in the message pane. If you do not see the button, follow this
    link to open the suggestion in the Microsoft Web-based Newsreader and then
    click "I Agree" in the message pane.

    http://www.microsoft.com/communitie...&dg=microsoft.public.internetexplorer.general
     
  2. PA Bear [MS MVP]

    PA Bear [MS MVP] Flightless Bird

    MSDN IE Development Forum (post such questions here instead)
    http://social.msdn.microsoft.com/Forums/en-US/iewebdevelopment/threads


    _JM_ wrote:
    > I wrote a piece of code that shows the problem.
    > When you delete a member of an object with a delete operator, you should
    > be
    > able to see the prototype's value of the same property name if it exists
    > (because of chaining).
    >
    > This works fine unless you try to do a for in statement of the object
    > after
    > the delete, in which case the value of the prototype is not returned, in
    > fact it's never retrieved at all.
    >
    > The example has two instances of Bar, both of them should have 3 members
    > before and after the delete, but after the delete only 2 members are found
    > on one of the instances.
    >
    > This very example works fine in all other browsers (Firefox, Chrome,
    > Opera).
    > If you do almost anything with the bar variable after the delete an before
    > the for in (for example uncomment the comparation bar==0) then the for in
    > statement works fine, just like it should always work.
    >
    > This bug could produce some very serious problems on code that would seem
    > to
    > work under most common cirumstances.
    >
    > Here's the code:
    >
    > str="";
    > var display = function(nombre, obj)
    > {
    > var i = 0;
    > str+:-("<br/>"+nombre+"<br/>");
    > for (var a in obj){
    > str+:-("&nbsp&nbsp "+a + "="+ obj[a]+"<br/>");
    > i++;
    > }
    > str+:-("<br/>"+i+"<br/>");
    > }
    >
    > var Bar = function()
    > {
    > this.instMember = "bar";
    > }
    >
    > Bar.prototype.name = "proto";
    > Bar.prototype.id = 3;
    >
    > bar = new Bar();
    > bar2 = new Bar();
    > bar.name = "inst";
    >
    > //display objects before delete
    > display("bar", bar);
    > display("bar2", bar2);
    >
    > str+:-("<br/>----- delete bar.name<br/>");
    > delete bar.name;
    > //bar==0;
    >
    > //display objects after delete
    > display("bar", bar);
    > display("bar2", bar2);
    >
    > document.write(str);
    >
    >
    > ----------------
    > This post is a suggestion for Microsoft, and Microsoft responds to the
    > suggestions with the most votes. To vote for this suggestion, click the "I
    > Agree" button in the message pane. If you do not see the button, follow
    > this
    > link to open the suggestion in the Microsoft Web-based Newsreader and then
    > click "I Agree" in the message pane.
    >
    > http://www.microsoft.com/communitie...&dg=microsoft.public.internetexplorer.general
     

Share This Page