创建对象有两种创建在JavaScript对象的方法有两种:新主场迎战{}在JavaScript
- 一个“构造函数”
- 回报的字典{使用新的},并设置适当的键/值对
首先是,例如
FooType = function() {
this.hello = function() { alert("hello"); };
};
foo = new FooType();
foo.hello();
第二是
fooFactory = function() {
return {
hello : function() { alert("hello"); }
};
};
foo = fooFactory();
foo.hello();
(为该帖子写的代码。不保证是正确的)
除了有这个绑定到全局对象的错误的风险,这两个方法是完全等价的吗(也考虑原型继承等)?
见http://stackoverflow.com/questions/444170/object-oriented-questions-in-javascript – 2010-01-04 13:49:44