在另一个问题中,一个用户指出使用new关键字是危险的,并提出了一个不使用new的对象创建解决方案。我不相信这是真的,主要是因为我使用过Prototype、Script.aculo.us和其他优秀的JavaScript库,它们都使用new关键字。
尽管如此,昨天我在YUI剧院看了Douglas Crockford的演讲,他说了完全相同的事情,他不再在他的代码中使用new关键字(Crockford谈JavaScript -第三幕:函数终极- 50:23分钟)。
使用new关键字“不好”吗?使用它的优点和缺点是什么?
以下是我对支持和反对使用新运算符的两个最有力的论点所做的最简短的总结:
反对新建的理由
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的文章,以获得对这种技术的简单解释,以及对他所倡导的继承模型的一般更深入的解释。
我是JavaScript的新手,所以可能我只是没有太多的经验来提供一个好的观点。然而,我想分享一下我对这个“新”事物的看法。
我来自c#世界,在那里使用关键字“new”是如此自然,以至于工厂设计模式对我来说看起来很奇怪。
当我第一次用JavaScript编码时,我没有意识到有“new”关键字和像YUI模式那样的代码,很快我就陷入了灾难。当回顾我所编写的代码时,我不知道某一行应该做什么。更混乱的是,当我“干运行”代码时,我的思维不能真正地在对象实例边界之间转换。
然后,我发现“新”关键字,对我来说,“分离”的东西。使用new关键字,它创建东西。如果没有new关键字,我知道我不会把它与创建东西混淆,除非我调用的函数提供了强有力的线索。
例如,用var bar=foo();我没有任何线索,什么酒吧可能是....它是一个返回值还是一个新创建的对象?但是用var bar = new foo();我确定bar是一个对象。
不使用new关键字的理由很简单:
通过根本不使用它,您可以避免意外遗漏它的陷阱。YUI使用的构造模式是一个如何完全避免new关键字的例子:
var foo = function () {
var pub = { };
return pub;
}
var bar = foo();
或者,你可以这样做:
function foo() { }
var bar = new foo();
但是这样做有可能会有人忘记使用new关键字,并且this操作符完全是FUBAR。据我所知,这样做没有任何好处(除了你已经习惯了)。
在一天结束的时候:这是关于防御性的。你会用这个新语句吗?是的。这会让你的代码更危险吗?是的。
如果你曾经写过c++,这类似于在删除指针后将它们设置为NULL。
以下是我对支持和反对使用新运算符的两个最有力的论点所做的最简短的总结:
反对新建的理由
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的文章,以获得对这种技术的简单解释,以及对他所倡导的继承模型的一般更深入的解释。