在另一个问题中,一个用户指出使用new关键字是危险的,并提出了一个不使用new的对象创建解决方案。我不相信这是真的,主要是因为我使用过Prototype、Script.aculo.us和其他优秀的JavaScript库,它们都使用new关键字。
尽管如此,昨天我在YUI剧院看了Douglas Crockford的演讲,他说了完全相同的事情,他不再在他的代码中使用new关键字(Crockford谈JavaScript -第三幕:函数终极- 50:23分钟)。
使用new关键字“不好”吗?使用它的优点和缺点是什么?
我写了一篇关于如何缓解不使用new关键字调用构造函数的问题的文章。
它主要是说教性的,但它展示了如何创建使用或不使用new的构造函数,并且不需要在每个构造函数中添加样板代码来测试它。
不使用new的构造函数
以下是该技巧的要点:
/**
* Wraps the passed in constructor so it works with
* or without the new keyword
* @param {Function} realCtor The constructor function.
* Note that this is going to be wrapped
* and should not be used directly
*/
function ctor(realCtor) {
// This is going to be the actual constructor
return function wrapperCtor() {
var obj; // The object that will be created
if (this instanceof wrapperCtor) {
// Called with new
obj = this;
} else {
// Called without new. Create an empty object of the
// correct type without running that constructor
surrogateCtor.prototype = wrapperCtor.prototype;
obj = new surrogateCtor();
}
// Call the real constructor function
realCtor.apply(obj, arguments);
return obj;
}
function surrogateCtor() {}
}
下面是如何使用它:
// Create our point constructor
Point = ctor(function(x, y) {
this.x = x;
this.y = y;
});
// This is good
var pt = new Point(20, 30);
// This is OK also
var pt2 = Point(20, 30);
JavaScript是一种动态语言,有无数种方法可以把其他语言阻止你的事情搞砸。
避免使用像new这样的基本语言特性,因为你可能会搞砸,这有点像在穿过雷区之前脱掉你闪闪发光的新鞋,以防你的鞋被弄脏。
我使用了一种约定,函数名以小写字母开头,而实际上是类定义的“函数”以大写字母开头。结果是一个非常引人注目的视觉线索,“语法”是错误的:
var o = MyClass(); // This is clearly wrong.
最重要的是,良好的命名习惯会有所帮助。毕竟,函数是做事情的,因此它的名字中应该有一个动词,而类代表对象,是没有任何动词的名词和形容词。
var o = chair() // Executing chair is daft.
var o = createChair() // Makes sense.
Stack Overflow的语法着色是如何解释上面的代码的,这很有趣。
我是JavaScript的新手,所以可能我只是没有太多的经验来提供一个好的观点。然而,我想分享一下我对这个“新”事物的看法。
我来自c#世界,在那里使用关键字“new”是如此自然,以至于工厂设计模式对我来说看起来很奇怪。
当我第一次用JavaScript编码时,我没有意识到有“new”关键字和像YUI模式那样的代码,很快我就陷入了灾难。当回顾我所编写的代码时,我不知道某一行应该做什么。更混乱的是,当我“干运行”代码时,我的思维不能真正地在对象实例边界之间转换。
然后,我发现“新”关键字,对我来说,“分离”的东西。使用new关键字,它创建东西。如果没有new关键字,我知道我不会把它与创建东西混淆,除非我调用的函数提供了强有力的线索。
例如,用var bar=foo();我没有任何线索,什么酒吧可能是....它是一个返回值还是一个新创建的对象?但是用var bar = new foo();我确定bar是一个对象。
以下是我对支持和反对使用新运算符的两个最有力的论点所做的最简短的总结:
反对新建的理由
Functions designed to be
instantiated as objects using the
new operator can have disastrous
effects if they are incorrectly
invoked as normal functions. A
function's code in such a case will
be executed in the scope where the
function is called, instead of in
the scope of a local object as
intended. This can cause global
variables and properties to get
overwritten with disastrous
consequences.
Finally, writing function Func(),
and then calling Func.prototype
and adding stuff to it so that you
can call new Func() to construct
your object seems ugly to some
programmers, who would rather use
another style of object inheritance
for architectural and stylistic
reasons.
关于这个争论的更多信息,请参阅Douglas Crockford的著作《JavaScript: The Good Parts》。事实上,还是去看看吧。
赞成新
使用新的运算符
原型赋值很快。
那些关于意外的东西
运行一个构造函数
全局名称空间中的代码可以
很容易预防,如果你总是
包含一些代码在您的
要检查的构造函数
看看它们是否被调用
正确,而且,在这种情况下
他们没有,接听电话
如你所愿。
请参阅John Resig的文章,以获得对这种技术的简单解释,以及对他所倡导的继承模型的一般更深入的解释。