Why does the parentheses have to follow immediately after the new Function? The MSDN website was unclear regarding why this is an error.
// 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. The new
operator is calling the constructor and generating the alert. The greetings()
line actually throws a type error since at that point greetings is just an object (an instance of 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.