IE and named function expressions

R

RobG

I'm sure this has been discussed before but I can't find it. The
following code:

var foo;
(function() {
foo = function foo(){};
}());

alert(typeof foo);

shows "function" in most browsers but "undefined" in IE. It appears
that IE is creating a local variable foo inside the anonymous function
as a consequence of the named function expression. So the function is
assigned to a local variable and goes out of scope for the alert. But
in non-IE browsers, foo resolves to the global foo and is in scope for
the alert.

Is this consistent with ECMA-262 or not? I'm simply not up to wading
through the spec in sufficient detail to answer the question myself.
 
R

RobG

I'm sure this has been discussed before but I can't find it. The
following code:

  var foo;
  (function() {
    foo = function foo(){};
  }());

  alert(typeof foo);

shows "function" in most browsers but "undefined" in IE. It appears
that IE is creating a local variable foo inside the anonymous function
as a consequence of the named function expression. So the function is
assigned to a local variable and goes out of scope for the alert. But
in non-IE browsers, foo resolves to the global foo and is in scope for
the alert.

Is this consistent with ECMA-262 or not? I'm simply not up to wading
through the spec in sufficient detail to answer the question myself.

Managed to stumble across a reasonable article by Juriy "kangax"
Zaytsev that goes through a number of related issues: <URL:
http://kangax.github.com/nfe/#jscript-bugs >
 
D

dhtml

I'm sure this has been discussed before but I can't find it. The
following code:

  var foo;
  (function() {
    foo = function foo(){};
  }());

  alert(typeof foo);

shows "function" in most browsers but "undefined" in IE.
I complained to MS with that same example years ago and I they've
since fixed it.
http://blogs.msdn.com/b/jscript/archive/2007/10/29/ecmascript-3-and-beyond.aspx

Should only apply to older versions of IE before IE9 :)

It appears
that IE is creating a local variable foo inside the anonymous function
as a consequence of the named function expression. So the function is
assigned to a local variable and goes out of scope for the alert. But
in non-IE browsers, foo resolves to the global foo and is in scope for
the alert.
JScript saw "function foo" as a FunctionExpression and bound "foo" to
the VO.

That aberrant behavior was explained in MS-EX3 and that document is
linked from the FAQ: http://jibbering.com/faq/#ecmaResources
here: http://msdn.microsoft.com/en-us/library/ff520996(VS.85).aspx
Is this consistent with ECMA-262 or not? I'm simply not up to wading
through the spec in sufficient detail to answer the question myself.

This is a JScript deviation.

See also:
http://www.highdots.com/forums/javascript/ms-releases-document-jscript-deviations-297353.html
 
D

dhtml

Was it a link pointing here:
"http://yura.thinkweb2.com/named-function-expressions/"?

I've seen that link on various pages of the FAQ. I can update my copy.
Yes, those thinkweb2 links are no good. The FAQ is on Github:
https://github.com/hij1nx/dhtmlkitchen/tree/master/dhtmlkitchen

The FAQ should be an independent project on Github. That way others
can fork, modify, and issue pull requests. Anyone's free to create an
FAQ project for this NG and if and when dhtmlkitchen goes back up, web
version of the FAQ (not the text version, which is for news) can go
there.
 
D

Dr J R Stockton

In comp.lang.javascript message <78ec5d8d-c318-40ee-ad2a-ca0791074e3c@p3
5g2000prp.googlegroups.com>, Mon, 17 Oct 2011 23:33:41, dhtml
Yes, those thinkweb2 links are no good. The FAQ is on Github:
https://github.com/hij1nx/dhtmlkitchen/tree/master/dhtmlkitchen

The FAQ should be an independent project on Github. That way others
can fork, modify, and issue pull requests. Anyone's free to create an
FAQ project for this NG and if and when dhtmlkitchen goes back up, web
version of the FAQ (not the text version, which is for news) can go
there.

A newsgroup FAQ should be the edited work of the regulars of the
newsgroup. It must not be open to editing by anyone who wants to play.
It should be shown, regularly, in the newsgroup either as a whole or in
part.

The FAQ text must be readily and directly accessible for reading by the
ignorant and inexperienced. The URL that you gave does not provide
that.

There is no need to wait for dhmlkitchen or jibbering in order to get
the HTML FAQ; it is, as I have often said before, available at merlyn as
$clj-faq.htm. For bandwidth reasons, please do not link to it there.

A FAQ for JavaScript such as you describe can be started at Github,
whatever that is, but it cannot there be a CLJ newsgroup FAQ. Can you
give an example URL for a FAQ of a computing language (not JavaScript or
jQuery, etc.) at Github - the readers' URL, nothing to do with editing?
 
A

Antony Scriven

[dhtml wrote:]
The FAQ should be an independent project on Github.
That way others can fork, modify, and issue pull
requests. Anyone's free to create an FAQ project for
this NG and if and when dhtmlkitchen goes back up, web
version of the FAQ (not the text version, which is for
news) can go there.

A newsgroup FAQ should be the edited work of the regulars
of the newsgroup. It must not be open to editing by
anyone who wants to play. It should be shown, regularly,
in the newsgroup either as a whole or in part.

Yep. This doesn't preclude Github.
The FAQ text must be readily and directly accessible for
reading by the ignorant and inexperienced. The URL that
you gave does not provide that.

True. But using the information here

http://pages.github.com/

would make it properly accessible (I think; I've not tried
it personally). If this can be practicably used for the FAQ
then I think it would be more appealing than a privately
hosted version.
A FAQ for JavaScript such as you describe can be started
at Github, whatever that is, but it cannot there be a CLJ
newsgroup FAQ.

Why? If you mean solely having the FAQ reside in a version
control system, then sure, but Github does more than that.
Can you give an example URL for a FAQ of a computing
language (not JavaScript or jQuery, etc.) at Github - the
readers' URL, nothing to do with editing?

http://defunkt.github.com/resque

Not a FAQ, but that doesn't matter. --Antony
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top