[英]How does the new operator work in JavaScript?
Probably the least understood part of JavaScript, standing beside the prototype chain.可能是 JavaScript 中最不为人知的部分,站在原型链旁边。
So the question is: how does...那么问题来了:怎么...
new dataObj(args);
...actually create an object, and define its prototype chain/constructors/etc? ...实际上创建一个 object,并定义其原型链/构造函数/等?
Best is to show an alternative, to fully understand this keyword.最好是显示一个替代方案,以完全理解这个关键字。
The new
operator uses the internal [[Construct]]
method, and it basically does the following: new
运算符使用内部的[[Construct]]
方法,它基本上做了以下工作:
[[Prototype]]
of this object, pointing to the Function prototype
property.[[Prototype]]
,指向 Function prototype
属性。
prototype
property is not an object (a primitive values, such as a Number, String, Boolean, Undefined or Null), Object.prototype
is used instead.prototype
属性不是 object(原始值,例如数字、字符串、Boolean、未定义或 Null),则使用Object.prototype
。this
value.this
值。 An equivalent implementation of what the new
operator does, can be expressed like this (assuming that the ECMAScript 5 Object.create
method is available): new
运算符的等效实现可以这样表示(假设 ECMAScript 5 Object.create
方法可用):
function NEW(f) {
var obj, ret, proto;
// Check if `f.prototype` is an object, not a primitive
proto = Object(f.prototype) === f.prototype ? f.prototype : Object.prototype;
// Create an object that inherits from `proto`
obj = Object.create(proto);
// Apply the function setting `obj` as the `this` value
ret = f.apply(obj, Array.prototype.slice.call(arguments, 1));
if (Object(ret) === ret) { // the result is an object?
return ret;
}
return obj;
}
// Example usage:
function Foo (arg) {
this.prop = arg;
}
Foo.prototype.inherited = 'baz';
var obj = NEW(Foo, 'bar');
obj.prop; // 'bar'
obj.inherited; // 'baz'
obj instanceof Foo // true
The expression new C(arg1, arg2)
:表达式
new C(arg1, arg2)
:
Assuming C is a JavaScript function (otherwise you get an error):假设 C 是 JavaScript function (否则会出现错误):
prototype
" property of C
.C
的“ prototype
”属性的值。
prototype
for a function is an object (automatically created when the function is declared) with its prototype set to Object.prototype
and a constructor
property pointing back to the function C
. prototype
for a function is an object (automatically created when the function is declared) with its prototype set to Object.prototype
and a constructor
property pointing back to the function C
.C
with ' this
' set to the new object, and with the supplied arguments.C
并将“ this
”设置为新的 object,并使用提供的 ZDBC16FB4CAA5BDA8EDDAZ77。C
returns an object, this object is the result of the expression.C
返回 object,则此 ZA8CFDE6331BD59EB2AC96F8911C4B6 的结果为。 Otherwise the newly created object is the result of the expression. An alternative to new
in ECMAScript 5 would be to use the builtin Object.createObject
method. ECMAScript 5 中
new
的替代方法是使用内置的Object.createObject
方法。
new C(arg1, arg2)
would be equivalent to: new C(arg1, arg2)
相当于:
var obj = Object.createObject(C.prototype);
C.apply(obj, [arg1, arg2]);
Standard JavaScript does not allow you to explicitly set the prototype of an object, so Object.createObject
cannot be implemented in the language itself.标准 JavaScript 不允许您显式设置 object 的原型,因此
Object.createObject
无法在语言本身中实现。 Some implementations does allow it through the non-standard property __proto__.一些实现确实允许它通过非标准属性 __proto__。 In that case,
new C
can be simulated like this:在这种情况下,可以像这样模拟
new C
:
var obj = {};
obj.__proto__ = C.prototype;
C.apply(obj, [arg1, arg2]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.