MDN Function.prototype.apply()的一个例子 [英] an example of MDN Function.prototype.apply()
问题描述
以下示例来自MDN:
Function.prototype.construct = function(aArgs) {
console.warn('Function construct')
var oNew = Object.create(this.prototype);
this.apply(oNew, aArgs);
return oNew;
};
function MyConstructor() {
console.warn('MyConstructor')
for (var nProp = 0; nProp < arguments.length; nProp++) {
this["property" + nProp] = arguments[nProp];
}
}
var myArray = [4, "Hello world!", false];
var myInstance = MyConstructor.construct(myArray);
结果是:
Function construct
MyConstructor
为什么 this.apply(oNew,aArgs);
叫 MyConstructor()
?
谢谢你的回答
推荐答案
为什么
this.apply(oNew,aArgs);
名为MyConstructor()
?
让我们一步一步走:
-
当解释器到达
MyConstructor.construct(myArray);
时,它会尝试查找属性<$ c对象MyConstructor
上的$ c>构造。口译员找不到它。然后它通过其原型链(内部[[Prototype]]
属性),即对象Function.prototype
。它有它!查找过程停止并开始解释构造
函数。这种查找属性的方式称为原型继承。
When the interpreter reaches to
MyConstructor.construct(myArray);
, it tries to find propertyconstruct
on objectMyConstructor
. The interpreter cannot find it. It then goes one step through its prototype chain (the internal[[Prototype]]
property), i.e. the objectFunction.prototype
. It does have it! The lookup process stops and start interpretingconstruct
function. This way of looking up properties is called prototype inheritance.
在这种情况下(并非总是),在
构造内
function指的是 MyConstructor
对象(记住所有函数都是对象)。所以 this.prototype
是对象 Function.prototype
。
In this case (not always), this
inside that construct
function refers to the MyConstructor
object (remember all functions are objects). So this.prototype
is the object Function.prototype
.
Object.create()
获取一个对象并创建一个新对象,并将给定对象作为其直接父原型对象。所以 oNew
是一个普通的对象,其原型与相同
。
Object.create()
gets an object and creates a new object with the given object as its immediate parent prototype object. So oNew
is an ordinary object having the same prototype as this
.
this.apply(oNew,aArgs);
这里记得这个
是 MyConstructor
。所以这个语句调用 MyConstructor
函数并传递 oNew
作为这个
和 aArgs
作为其参数。
this.apply(oNew, aArgs);
Here recall that this
is MyConstructor
. So this statement invokes MyConstructor
function and passes oNew
as its this
and aArgs
as its arguments.
当 MyConstructor $ c时$ c>被调用其
此
指的是 oNew
及其参数
是一个包含 aArgs
的数组。
When MyConstructor
is invoked its this
refers to oNew
and its arguments
is an array containing aArgs
.
第二个例子:
什么是 [] .push.apply(args,arguments);
?我们走吧:)
-
[]
创建一个空数组文字(这是一个对象也是如此。 - 由于
[]
本身没有任何属性,[]。 push
通过其原型链引用其父级的push
对象(记住函数是对象)。 -
[] .push.apply
调用该函数并传递args
作为其this
和参数
作为参数。
[]
creates an empty array literal (which is an object too).- Since
[]
on its own doesn't have any properties,[].push
refers to thepush
object (remember functions are objects) on its parent through its prototype chain. [].push.apply
invokes that function and passesargs
as itsthis
andarguments
as its arguments.
你可以改为 []。push.apply()
使用 Array.prototype.push.apply()
。那些 push
对象引用同一个对象。
You can instead of [].push.apply()
use Array.prototype.push.apply()
. Those push
objects refer to the same object.
这篇关于MDN Function.prototype.apply()的一个例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!