By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,569 Members | 1,181 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,569 IT Pros & Developers. It's quick & easy.

Why these codes not worked well in IE7.

P: n/a
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??

Sep 20 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

Sep 20 '06 #2

P: n/a
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 '06 #3

P: n/a

jh*****@gmail.com 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

Sep 20 '06 #4

P: n/a

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

Sep 20 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.