Internet explorer 为什么IE6会给出一个“答案”;“预期功能”;将变量声明为函数对象的新实例时出错?

Internet explorer 为什么IE6会给出一个“答案”;“预期功能”;将变量声明为函数对象的新实例时出错?,internet-explorer,internet-explorer-6,javascript,Internet Explorer,Internet Explorer 6,Javascript,为什么新函数后面必须紧跟括号?MSDN网站不清楚这是一个错误的原因 // Fails but only in IE6 var greetings = new SayHello; greetings(); // This works in IE6 var salutations = new SayHello(); function SayHello() { alert("Hello"); }; 我不认为代码做了你认为它做的。试着这样做: var greetings = new SayHel

为什么新函数后面必须紧跟括号?MSDN网站不清楚这是一个错误的原因

// Fails but only in IE6
var greetings = new SayHello;
greetings();

// This works in IE6
var salutations = new SayHello();

function SayHello() {
 alert("Hello");
};

我不认为代码做了你认为它做的。试着这样做:

var greetings = new SayHello;
alert('calling the constructor');
greetings();

function SayHello() {
 alert("Hello");
};
您将首先看到“Hello”警报,然后是“calling the constructor”警报,我认为这与您预期的相反。
new
操作员正在调用构造函数并生成警报。
greetings()
行实际上抛出了一个类型错误,因为此时greetings只是一个对象(SayHello的一个实例)。我猜(因为我没有副本),IE6只是在括号丢失时没有调用构造函数,所以它似乎以不同的方式被破坏了