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?
例如,如果我们使用new关键字进行比较,使对象上的函数集属性和方法只是让函数返回一个新对象,那么新对象有什么额外的作用吗?
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?
firebug探测器告诉我使用new关键字稍微快一点(2ms而不是3ms),对大型对象来说新的仍然明显更快?
[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?
另一个问题是真正大的对象构造函数在函数的底部有一个返回(它将具有大量的本地函数)或者在函数的顶部有一些this.bar = ...更具可读性?什么被认为是一个好的约定?
var MAIN = newfunction() { this.bar = ... // Lots of code}();var MAIN2 = function() { // Lots of code return { bar: ... }}();
2 个解决方案
#1
16
Quoting Douglas Crockford from the Good Parts book (page 47), to answer the title of this question:
引用道格拉斯克罗克福德的Good Parts书(第47页)来回答这个问题的标题:
If the
new
operator were a method instead of an operator, it could be implemented like this:如果new运算符是一个方法而不是一个运算符,它可以像这样实现:
Function.method('new', function () { // Create a new object that inherits from the // constructor's prototype. var that = Object.create(this.prototype); // Invoke the constructor, binding -this- to // the new object. var other = this.apply(that, arguments); // If its return value isn't an object, // substitute the new object. return (typeof other === 'object' && other) || that;});
The Function.method
method is implemented as follows. This adds an instance method to a class (Source):
Function.method方法实现如下。这为类(Source)添加了一个实例方法:
Function.prototype.method = function (name, func) { this.prototype[name] = func; return this;};
Further reading:
- Mozilla Dev Center:
Object.create()
- Mozilla Dev Center:
Function.apply()
- Douglas Crockford: Classical Inheritance in JavaScript
Mozilla开发中心:Object.create()
Mozilla开发中心:Function.apply()
Douglas Crockford:JavaScript中的经典继承
#2
6
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).
阅读规范。第11.2.2和13.2.2节是相关的,并且不太难理解(注意后两个链接是非官方的HTML-ified版本的规范)。
In summary, if you have a function f
that returns an object, the only observable difference that calling it with new
will make is that the this
value will be different, and that calling it with new
may be slower, since it involves additional steps of creating an object and assigning it a few properties.
总之,如果你有一个返回一个对象的函数f,用new调用它的唯一可观察的差异就是这个值会有所不同,并且用new调用它可能会更慢,因为它涉及额外的步骤创建一个对象并为其分配一些属性。
#1
16
Quoting Douglas Crockford from the Good Parts book (page 47), to answer the title of this question:
引用道格拉斯克罗克福德的Good Parts书(第47页)来回答这个问题的标题:
If the
new
operator were a method instead of an operator, it could be implemented like this:如果new运算符是一个方法而不是一个运算符,它可以像这样实现:
Function.method('new', function () { // Create a new object that inherits from the // constructor's prototype. var that = Object.create(this.prototype); // Invoke the constructor, binding -this- to // the new object. var other = this.apply(that, arguments); // If its return value isn't an object, // substitute the new object. return (typeof other === 'object' && other) || that;});
The Function.method
method is implemented as follows. This adds an instance method to a class (Source):
Function.method方法实现如下。这为类(Source)添加了一个实例方法:
Function.prototype.method = function (name, func) { this.prototype[name] = func; return this;};
Further reading:
- Mozilla Dev Center:
Object.create()
- Mozilla Dev Center:
Function.apply()
- Douglas Crockford: Classical Inheritance in JavaScript
Mozilla开发中心:Object.create()
Mozilla开发中心:Function.apply()
Douglas Crockford:JavaScript中的经典继承
#2
6
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).
阅读规范。第11.2.2和13.2.2节是相关的,并且不太难理解(注意后两个链接是非官方的HTML-ified版本的规范)。
In summary, if you have a function f
that returns an object, the only observable difference that calling it with new
will make is that the this
value will be different, and that calling it with new
may be slower, since it involves additional steps of creating an object and assigning it a few properties.
总之,如果你有一个返回一个对象的函数f,用new调用它的唯一可观察的差异就是这个值会有所不同,并且用new调用它可能会更慢,因为它涉及额外的步骤创建一个对象并为其分配一些属性。