当地参考这个?
local reference to this?
为什么 'this' 需要本地引用?第一个代码段工作正常,但第二个代码段出现错误...您正在创建一个引用(不是按值传递),因此您将修改相同的对象原型,这正是您尝试做的事情。 .. 不确定错误来自何处...
代码的区别在于每个段的前几行
第一个代码段:
Function.prototype.construct = function(aArgs) {
var a = this;
var fNewConstr = function() { a.apply(this, aArgs); };
fNewConstr.prototype = a.prototype;
return new fNewConstr();
};
function 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.prototype.construct = function(aArgs) {
var fNewConstr = function() { this.apply(this, aArgs); };
fNewConstr.prototype = this.prototype;
return new fNewConstr();
};
function 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);
在代码的第一位,您的 "construct" 方法中,this
的值将是您调用它的函数。 不是函数打算从其内部构造函数return新建的对象。
this
的值是为每个函数调用设置的,具体取决于调用的性质。您通过 new
调用 "fNewConstr",因此该函数中 this
的值是正在构造的对象。为了正确地将原始 "root" 函数应用到新创建的对象,它需要对该函数的引用。这就是 "a".
中保存的内容
一步一步,当你打电话
MyConstructor.construct(myArray);
发生的事情是:
- 检查符号 "MyConstructor" 发现它是对一个对象的引用——实际上是一个函数。
-
.
运算符导致 属性 "construct" 被查找,首先是 "MyConstructor" 函数对象本身,然后是对象的原型,其中 属性 将找到名称。
- "construct" 属性 的值是一个函数,因此
( )
运算符启动函数调用。
- 现在我们在 "construct" 中,
this
是对函数 "MyConstructor" 的引用。该引用立即复制到 "a".
- "fNewConstr" 变量被初始化,然后立即通过
new
调用。创建了一个新对象,现在 启动了那个 函数调用,this
引用了那个新对象。
- 在 "fNewConstr" 中,"a" 仍然引用 "MyConstructor",因此该引用让运行时在 Function 原型上找到 "apply" 函数。现在 那个 函数启动了,我们最终会在 "MyConstructor" 代码中结束。
- 在"MyConstructor"里面,
this
会引用new
操作构造的对象,参数是原来传入的数组元素。
为什么 'this' 需要本地引用?第一个代码段工作正常,但第二个代码段出现错误...您正在创建一个引用(不是按值传递),因此您将修改相同的对象原型,这正是您尝试做的事情。 .. 不确定错误来自何处...
代码的区别在于每个段的前几行
第一个代码段:
Function.prototype.construct = function(aArgs) {
var a = this;
var fNewConstr = function() { a.apply(this, aArgs); };
fNewConstr.prototype = a.prototype;
return new fNewConstr();
};
function 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.prototype.construct = function(aArgs) {
var fNewConstr = function() { this.apply(this, aArgs); };
fNewConstr.prototype = this.prototype;
return new fNewConstr();
};
function 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);
在代码的第一位,您的 "construct" 方法中,this
的值将是您调用它的函数。 不是函数打算从其内部构造函数return新建的对象。
this
的值是为每个函数调用设置的,具体取决于调用的性质。您通过 new
调用 "fNewConstr",因此该函数中 this
的值是正在构造的对象。为了正确地将原始 "root" 函数应用到新创建的对象,它需要对该函数的引用。这就是 "a".
一步一步,当你打电话
MyConstructor.construct(myArray);
发生的事情是:
- 检查符号 "MyConstructor" 发现它是对一个对象的引用——实际上是一个函数。
-
.
运算符导致 属性 "construct" 被查找,首先是 "MyConstructor" 函数对象本身,然后是对象的原型,其中 属性 将找到名称。 - "construct" 属性 的值是一个函数,因此
( )
运算符启动函数调用。 - 现在我们在 "construct" 中,
this
是对函数 "MyConstructor" 的引用。该引用立即复制到 "a". - "fNewConstr" 变量被初始化,然后立即通过
new
调用。创建了一个新对象,现在 启动了那个 函数调用,this
引用了那个新对象。 - 在 "fNewConstr" 中,"a" 仍然引用 "MyConstructor",因此该引用让运行时在 Function 原型上找到 "apply" 函数。现在 那个 函数启动了,我们最终会在 "MyConstructor" 代码中结束。
- 在"MyConstructor"里面,
this
会引用new
操作构造的对象,参数是原来传入的数组元素。