[英]Why does IE6 give a “Function expected” error when declaring a variable as an new instance of a function Object?
Why does the parentheses have to follow immediately after the new Function? 为什么在新功能之后必须紧跟括号? The MSDN website was unclear regarding why this is an error.
MSDN网站尚不清楚为什么这是一个错误。
// Fails but only in IE6
var greetings = new SayHello;
greetings();
// This works in IE6
var salutations = new SayHello();
function SayHello() {
alert("Hello");
};
I don't think that code does what you think it does. 我认为代码没有实现您想像的那样。 Try it this way:
尝试这种方式:
var greetings = new SayHello;
alert('calling the constructor');
greetings();
function SayHello() {
alert("Hello");
};
You will see the "Hello" alert first then the "calling the constructor" alert, which I think is the opposite of what you expect. 您将首先看到“ Hello”警报,然后是“正在调用构造函数”警报,我认为这与您期望的相反。 The
new
operator is calling the constructor and generating the alert. new
操作员正在调用构造函数并生成警报。 The greetings()
line actually throws a type error since at that point greetings is just an object (an instance of SayHello). greetings()
行实际上会引发类型错误,因为那时候greetings只是一个对象(SayHello的实例)。 I'm guessing (since I don't have a copy) that IE6 just isn't calling the constructor when the parenthesis are missing so it seems broken in a different way. 我猜(因为我没有副本),当缺少括号时,IE6只是没有调用构造函数,因此它似乎以不同的方式损坏了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.