A very weird problem occurred, when I was trying to enable a lib (Beard.js) to support the javascript template engine Haml.
Haml could not be loaded correctly. I tracked down the code and find out that Haml has never been loaded into the page. After a lot of try and fail, I happened to make it work. The weird thing I found is:
in origin Haml lib, it is:
var Haml;
(function(){
...
Haml = function(){ ... }
...
}());
I changed the code to :
var Haml;
(function(){
...
window.Haml = function(){ ... }
...
}());
then it works..
WHY??? Shouldn't Haml be automatically recognized as defined in the global scope?
Environment - IE8
Haml.js -
Bear.js -
-------------- UPDATE ---------------
in Haml.js, it is:
var Haml;
(function(){
...
Haml = function Haml(){ ... }
...
}());
I guess in the javascript, the statement "function Haml(){}" makes Haml a local var. However, why can Haml be loaded correctly in Firefox & Chrome????
A very weird problem occurred, when I was trying to enable a lib (Beard.js) to support the javascript template engine Haml.
Haml could not be loaded correctly. I tracked down the code and find out that Haml has never been loaded into the page. After a lot of try and fail, I happened to make it work. The weird thing I found is:
in origin Haml lib, it is:
var Haml;
(function(){
...
Haml = function(){ ... }
...
}());
I changed the code to :
var Haml;
(function(){
...
window.Haml = function(){ ... }
...
}());
then it works..
WHY??? Shouldn't Haml be automatically recognized as defined in the global scope?
Environment - IE8
Haml.js - https://github./creationix/haml-js
Bear.js - https://github./jspopisno1/Beard
-------------- UPDATE ---------------
in Haml.js, it is:
var Haml;
(function(){
...
Haml = function Haml(){ ... }
...
}());
I guess in the javascript, the statement "function Haml(){}" makes Haml a local var. However, why can Haml be loaded correctly in Firefox & Chrome????
Share Improve this question edited Oct 18, 2011 at 1:04 Liangliang Zheng asked Oct 18, 2011 at 0:40 Liangliang ZhengLiangliang Zheng 1,78511 silver badges16 bronze badges 7- 8 IE8 is stupid.... – please delete me Commented Oct 18, 2011 at 0:43
- Just speaking the truth. – please delete me Commented Oct 18, 2011 at 1:01
- @Mahnax - what is your explanation for why IE 8 is different to other browsers and how it is inconsistent with ECMA-262? If you don't have one, how do you know that it's not the other browsers that are "stupid"? – RobG Commented Oct 18, 2011 at 1:09
- 1 @RobG give you one nczonline/blog/2007/09/09/inconsistent-array-literals – Liangliang Zheng Commented Oct 18, 2011 at 1:14
- @RobG I use both IE8 and Safari 5 daily. Not only does Safari perform better in every way, it also looks nicer. – please delete me Commented Oct 18, 2011 at 1:16
3 Answers
Reset to default 5The problem is related to JScript (in versions up until IE8) having a bug whereby named function expressions leak into the enclosing scope. So named function expressions are parsed as function declarations (as well as function expressions), thereby automatically hoisting a local Haml
variable into the local scope. After that, you set Haml = function(){}
but that doesn't result in the global Haml
because JScript finds a local variable with that identifier because it incorrectly leaked into the local scope. So while the local Haml
is set correctly, the global one is never reached.
You can read about this more here.
> var Haml;
> (function(){
> ...
> Haml = function Haml(){ ... }
> ...
> }());
That code does not throw any errors in IE 8 for me. The part you are missing in your question is a following statement:
alert(typeof Haml);
which shows undefined in IE and function in Firefox and others.
The assignment to Haml is a named function expression (the name is optional) and yes, IE will create a variable in the current scope with the name, other browsers don't.
Shouldn't Haml be automatically recognized as defined in the global scope?
Only if that code itself is in the global scope. If it is part of a function, the var
will scope it to that function only.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744714301a4589525.html
评论列表(0条)