Crockford为推广优秀的JavaScript技术做了很多工作。他对语言关键要素的固执立场引发了许多有益的讨论。也就是说,有太多的人把每一个“坏”或“有害”的宣言都当作福音,拒绝超越一个人的观点。有时会让人有点沮丧。
使用new关键字提供的功能比从头构建每个对象有几个优点:
Prototype inheritance. While often looked at with a mix of suspicion and derision by those accustomed to class-based OO languages, JavaScript's native inheritance technique is a simple and surprisingly effective means of code re-use. And the new keyword is the canonical (and only available cross-platform) means of using it.
Performance. This is a side-effect of #1: if I want to add 10 methods to every object I create, I could just write a creation function that manually assigns each method to each new object... Or, I could assign them to the creation function's prototype and use new to stamp out new objects. Not only is this faster (no code needed for each and every method on the prototype), it avoids ballooning each object with separate properties for each method. On slower machines (or especially, slower JS interpreters) when many objects are being created this can mean a significant savings in time and memory.
是的,new有一个关键的缺点,其他答案巧妙地描述了它:如果你忘记使用它,你的代码会毫无警告地崩溃。幸运的是,这个缺点很容易减轻——只需在函数本身添加一些代码:
function foo()
{
// if user accidentally omits the new keyword, this will
// silently correct the problem...
if ( !(this instanceof foo) )
return new foo();
// constructor logic follows...
}
现在你可以拥有new的优势,而不必担心误用造成的问题。
John Resig在他的“简单类”实例化文章中详细介绍了这种技术,以及在默认情况下将这种行为构建到“类”中的方法。绝对值得一读……他即将出版的新书《JavaScript忍者的秘密》(Secrets of the JavaScript Ninja)也是如此,这本书发现了JavaScript语言中隐藏的黄金和许多其他“有害”特性(关于with的章节对我们这些最初将这个备受诟病的特性视为噱头而不屑一顾的人特别有启发)。
通用的健全性检查
您甚至可以在检查中添加一个断言,如果您不愿意让坏掉的代码默默地工作的话。或者,正如一些人评论的那样,使用check来引入一个运行时异常:
if ( !(this instanceof arguments.callee) )
throw new Error("Constructor called as a function");
请注意,这个代码片段能够避免硬编码构造函数名称,因为与前面的示例不同,它不需要实际实例化对象——因此,可以将它复制到每个目标函数中而无需修改。
ES5逃走
正如Sean McMillan, stephenbez和jrh所指出的,论点的使用。callee在ES5的严格模式下无效。因此,如果在该上下文中使用上述模式,它将抛出一个错误。
ES6和一个完全无害的新
ES6向JavaScript引入了类——不,不是像老派Crockford那样怪异地模仿java,而是在精神上更像他(和其他人)后来采用的轻量级方式,采用原型继承的最佳部分,并将公共模式烘焙到语言本身中。
...其中包括一个安全的新:
class foo
{
constructor()
{
// constructor logic that will ONLY be hit
// if properly constructed via new
}
}
// bad invocation
foo(); // throws,
// Uncaught TypeError: class constructors must be invoked with 'new'
但如果你不想用这种新糖怎么办?如果您只是想用上面所示的那种安全检查来更新您完美无缺的旧式原型代码,以便它们在严格模式下继续工作,该怎么办呢?
正如Nick Parsons指出的那样,ES6也提供了一个方便的检查,以new.target的形式:
function foo()
{
if ( !(new.target) )
throw new Error("Constructor called as a function");
// constructor logic follows...
}
所以无论你选择哪种方法,只要稍加思考和保持卫生,你就可以无害地使用新产品。