Why these codes not worked well in IE7.

H

Hatter

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??
 
R

RobG

Hatter said:
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 :)
 
J

jht5945

thanks Rob's answer.

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

it worked well like FF2b or Opera9
 
R

RobG

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.
 
R

RobG

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top