What's the difference between `f()` and `new f()`?
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
What is the 'new' keyword in JavaScript?
creating objects from JS closure: should i use the “new” keyword?
看到这段代码:
1 2 3 4 5 6 7 8 9 | function friend(name) { return { name: name }; } var f1 = friend('aa'); var f2 = new friend('aa'); alert(f1.name); // -> 'aa' alert(f2.name); // -> 'aa' |
?
您的案例中的新内容无用。
当函数使用'this'关键字时,您只需要使用new关键字。
1 2 3 4 5 6 7 8 9 10 11 12 13 | function f(){ this.a; } // new is required. var x = new f(); function f(){ return { a:1 } } // new is not required. var y = f(); |