Why these codes not worked well in IE7.

Discussion in 'Javascript' started by Hatter, Sep 20, 2006.

  1. Hatter

    Hatter Guest

    I wrote these codes:
    function aaa()
    {
    }
    aaa.nnnn = "now is aaa";
    alert(aaa.nnnn);

    eval("func = " + aaa.toString());

    alert(aaa.nnnn);
    func.nnnn = "now is func";
    alert(aaa.nnnn);
    alert(func.nnnn);

    is worked well in FF2b and Opera9, but it not worked in IE7.
    in FF2b and Opera9 it outputs:
    now is aaa
    now is aaa
    now is aaa
    now is func
    but in IE7 it outputs:
    now is aaa
    undefined
    undefined
    now is func

    will someone tell me why? IE7's bug??
     
    Hatter, Sep 20, 2006
    #1
    1. Advertising

  2. Hatter

    RobG Guest

    Hatter wrote:
    > I wrote these codes:
    > function aaa()
    > {
    > }
    > aaa.nnnn = "now is aaa";
    > alert(aaa.nnnn);
    >
    > eval("func = " + aaa.toString());
    >
    > alert(aaa.nnnn);
    > func.nnnn = "now is func";
    > alert(aaa.nnnn);
    > alert(func.nnnn);
    >
    > is worked well in FF2b and Opera9, but it not worked in IE7.
    > in FF2b and Opera9 it outputs:
    > now is aaa
    > now is aaa
    > now is aaa
    > now is func
    > but in IE7 it outputs:
    > now is aaa
    > undefined
    > undefined
    > now is func
    >
    > will someone tell me why?


    I can't tell you why, but I can explain what appears to be happening.
    Your eval statement is effectively:

    var func = function aaa(){};


    Which assigns a function object to func and gives it a name 'aaa'. In
    IE, an 'aaa' property is added to the global object which references
    what appears to be a copy or another instance of the same function
    object, replacing the previous 'aaa' property and giving the appearance
    of removing the public properties of aaa.

    A simpler test that does the same thing is:

    var func = function fred (){};
    alert( typeof window.fred); // Shows function in IE, undefined in Fx
    alert( func.toString() == fred.toString() ); // shows true in IE


    Note that in IE, func and fred are different (though virtually
    identical) objects, they are not references to the same object:

    alert( func == window.fred) // shows false
    func = '';
    alert(typeof func) // shows 'string'
    alert( typeof window.fred); // shows 'function'


    > IE7's bug??


    Dunno, is it a bug? Anyhow, the above is from IE 6 so it is not
    peculiar to IE 7 :)


    --
    Rob
     
    RobG, Sep 20, 2006
    #2
    1. Advertising

  3. Hatter

    Guest

    thanks Rob's answer.

    I found a solution that declare aaa function like this:
    aaa = function (){};

    it worked well like FF2b or Opera9
     
    , Sep 20, 2006
    #3
  4. Hatter

    RobG Guest

    wrote:
    > thanks Rob's answer.
    >
    > I found a solution that declare aaa function like this:
    > aaa = function (){};


    I guess we could quibble here and say that isn't a function
    declaration, it is initialising aaa with a function expression that
    creates an anonymous function object.

    function declaration: function aaa(){}
    function expression: var aaa = function(){}

    The difference is that in a function expression, the name is optional.
    It is interesting to note that in IE, function expressions with names
    are added as properties of the global object.


    --
    Rob
     
    RobG, Sep 20, 2006
    #4
  5. Hatter

    RobG Guest

    RobG wrote:
    [...]
    > The difference is that in a function expression, the name is optional.
    > It is interesting to note that in IE, function expressions with names
    > are added as properties of the global object.


    Here's an iteresting scenario:

    <script type="text/javascript">
    var b;
    var a = function b (){};
    alert(
    'a: ' + (typeof a)
    + '\nb: ' + (typeof b)
    + '\na=b? ' + (a==b)
    )
    </script>

    In IE (I tested 5.2 on Mac OS), a==b gives true, in other browsers b is
    undefined and hence show false.


    --
    Rob
     
    RobG, Sep 20, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bengt Richter
    Replies:
    0
    Views:
    297
    Bengt Richter
    Aug 7, 2003
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,995
    Smokey Grindel
    Dec 2, 2006
  3. Replies:
    2
    Views:
    2,843
    Malcolm
    Aug 20, 2005
  4. eliweiq001
    Replies:
    25
    Views:
    1,684
    Seebs
    Oct 2, 2010
  5. Replies:
    0
    Views:
    113
Loading...

Share This Page