无论如何,JavaScript中的`new`是什么意思? [英] What does `new` in JavaScript do, anyway?
问题描述
我对Javascrpt的构造函数如何工作感到非常困惑;尽管使用该语言已有好几年了(大多数情况下它就像是LISP的半命令版本),但我想更多地了解对象应该如何工作。
I am very confused about how constructors work in Javascrpt; despite using the language for several years (mostly as if it were like a semi-imperative version of LISP) I would like to know more about how objects are supposed to work in it.
鉴于此代码:
function Foo(x) {
return {
bar: function() { return x; }
};
}
调用 myFoo = Foo有什么区别? 5)
和 myFoo = new Foo(5)
?或者,换句话说,Javascript 中的构造函数究竟是什么?
What is the difference between calling myFoo = Foo(5)
and myFoo = new Foo(5)
? Or, in other words, what exactly does a constructor in Javascript do?
推荐答案
调用
myFoo = Foo(5)
和myFoo = new Foo(5)
之间的区别是什么? ?
What is the difference between calling
myFoo = Foo(5)
andmyFoo = new Foo(5)
?
该代码没有区别,因为它返回一个对象,并且规范说:
There's no difference for that code, because it returns an object, and the spec says:
- 让结果成为调用F的 [[Call]] 内部属性的结果,提供 obj 作为
这个
值并提供传递到 [[Construct]] 的参数列表作为args。
- 如果
类型(结果)
是对象
然后返回结果。
- Let result be the result of calling the [[Call]] internal property of F, providing obj as the
this
value and providing the argument list passed into [[Construct]] as args.- If
Type(result)
isObject
then return result.
由于该函数返回的是Object的结果,因此使用其结果。如果它没有返回一个对象,或者它检查了这个
,你会注意到一个区别,例如,如果你把它重写为:
Since that function returns a result that is an Object, its result is used. You would notice a difference if it did not return an object, or if it checked this
, for example if you rewrote it as:
function Foo(x) {
if (!(this instanceof Foo)) { return new Foo(x); }
this.bar = function() { return x; };
}
// Now instanceof works.
alert((new Foo) instanceof Foo);
new $ c $是什么c>在JavaScript中,无论如何?
What does
new
in JavaScript do, anyway?
new
运算符导致函数用调用此
绑定到新创建的 Object
,其原型是该函数的 prototype
property。
The new
operator causes the function to be called with this
bound to a newly created Object
whose prototype is that function's prototype
property.
对于用户定义的函数,
new f(a, b, c)
相当于
// Create a new instance using f's prototype.
var newInstance = Object.create(f.prototype), result;
// Call the function
result = f.call(newInstance, a, b, c),
// If the result is a non-null object, use it, otherwise use the new instance.
result && typeof result === 'object' ? result : newInstance
注意,语言规范实际上定义了两个操作的函数, [[Call]] 和 [[Construct]] ,所以在某些极端情况下 new
表现得很奇怪。
Note, that the language specification actually defines functions with two operations, [[Call]] and [[Construct]], so there are some corner cases where new
behaves oddly.
例如,绑定和内置函数:
For example, bound and built-in functions:
var g = f.call.bind(f);
应定义一个函数,在调用时,只需调用 f
,所以 g
应该在所有方面与 f
相同,但是
should define a function that when called, just calls f
, so g
should be the same as f
in all respects, but
new g()
生成
TypeError: function call() { [native code] } is not a constructor
因为内置函数 Function.prototype.call
支持 [[Call] ] 但不是 [[Construct]] 。
because the builtin function Function.prototype.call
supports [[Call]] but not [[Construct]].
Function.prototype.bind
在 new
和普通电话周围也有不同的表现。 此
值在调用时始终是绑定的thisValue,但是当您使用 new
时,它是一个新构造的实例。
Function.prototype.bind
also behaves differently around new
and regular calls. The this
value is always the bound thisValue when called, but is a newly constructed instance when you use new
.
这篇关于无论如何,JavaScript中的`new`是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!