new 关键字的作用是什么?
我很好奇除了改变 this
范围所指的内容之外,new
关键字在后台还能做什么。
例如,如果我们使用 new 关键字使函数在对象上设置属性和方法与仅使函数返回一个新对象进行比较,那么新对象有什么额外的作用吗?
如果我不想从函数构造函数创建多个对象,则首选哪个
var foo2 = function () {
var temp = "test";
return {
getLol: function () {
return temp;
},
setLol: function(value) {
temp = value;
}
};
}();
var foo = new function () {
var temp = "test";
this.getLol = function () {
return temp;
}
this.setLol = function(value) {
temp = value;
}
}();
Firebug Profiler 告诉我使用 new 关键字稍快(2 毫秒而不是 3 毫秒),在大型对象上, new 仍然明显更快吗?
[编辑]
另一件事是,对于真正的大型对象构造函数,在函数底部有一个 return (它将有大量的本地函数),或者在函数顶部有一些 this.bar = ...可读吗?什么被认为是一个好的约定?
var MAIN = newfunction() {
this.bar = ...
// Lots of code
}();
var MAIN2 = function() {
// Lots of code
return {
bar: ...
}
}();
I am curious as what else the new
keyword does in the background apart from changing what the this
scope refers too.
For example if we compare using the new
keyword to make a function set properties and methods on an object to just making a function return a new object, is there anything extra that the new object does?
And which is preferred if I don't wish to create multiple objects from the function constructor
var foo2 = function () {
var temp = "test";
return {
getLol: function () {
return temp;
},
setLol: function(value) {
temp = value;
}
};
}();
var foo = new function () {
var temp = "test";
this.getLol = function () {
return temp;
}
this.setLol = function(value) {
temp = value;
}
}();
The firebug profiler tells me using the new keyword is slightly faster (2ms instead of 3ms), on large objects is new still significantly faster?
[Edit]
Another matter is on really large object constructors is having a return at the bottom of the function (It will have a large amount of local functions) or having a few this.bar = ... at the top of the function more readable? What is considered a good convention?
var MAIN = newfunction() {
this.bar = ...
// Lots of code
}();
var MAIN2 = function() {
// Lots of code
return {
bar: ...
}
}();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
引用 Douglas Crockford 来自 Good Parts 书(第 47 页),回答这个问题的标题:
Function.method
方法实现如下。这会向类添加一个实例方法(源):进一步阅读:
Object.create()
Function.apply()
Quoting Douglas Crockford from the Good Parts book (page 47), to answer the title of this question:
The
Function.method
method is implemented as follows. This adds an instance method to a class (Source):Further reading:
Object.create()
Function.apply()
阅读规范。 11.2.2 和 13.2.2 是相关的,并且不太难以理解(请注意,后两个链接是非-规范的官方 HTML 版本)。
总之,如果您有一个返回对象的函数
f
,则使用new
调用它的唯一可观察到的区别是this
value 会有所不同,并且使用 new 调用它可能会更慢,因为它涉及创建对象并为其分配一些属性的额外步骤。Read the spec. Sections 11.2.2 and 13.2.2 are relevant and aren't too tricky to understand (note that the latter two links are to non-official HTML-ified version of the spec).
In summary, if you have a function
f
that returns an object, the only observable difference that calling it withnew
will make is that thethis
value will be different, and that calling it withnew
may be slower, since it involves additional steps of creating an object and assigning it a few properties.