Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Why these codes not worked well in IE7.

Reply
Thread Tools

Why these codes not worked well in IE7.

 
 
Hatter
Guest
Posts: n/a
 
      09-20-2006
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??

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      09-20-2006
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

 
Reply With Quote
 
 
 
 
jht5945@gmail.com
Guest
Posts: n/a
 
      09-20-2006
thanks Rob's answer.

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

it worked well like FF2b or Opera9

 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      09-20-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      09-20-2006

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why these codes will cause Segmentation fault in linux? eliweiq001 C Programming 25 10-02-2010 09:58 PM
Games worked before, why not now? VertiTech Windows 64bit 5 01-18-2008 11:24 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Virtual Key Codes, Scan Codes and ASCII Codes in C gj_williams2000@yahoo.co.uk C Programming 2 08-20-2005 11:04 AM
EOS plan worked well Godfrey DiGiorgi Digital Photography 0 09-22-2003 04:34 PM



Advertisments