对构造函数中当前对象的引用
Reference to current object in constructor
我试图定义一个类,该类在其构造函数中实例化其他对象,并将引用传递给它们自己:
var Child = function(m) {
var mother = m;
return {
mother: mother
}
}
var Mother = function() {
var children = makeChildren();
return {
children: children
}
function makeChildren() {
var children = [];
for (var i = 0; i < 10; i++) {
var c = new Child(this); // <--- 'this' is an empty object here
children.push(c)
}
return children;
}
}
这不起作用,并且Child实例的mother
属性中最终有一个空对象。做这件事的正确方法是什么?
Javascript的this
不是词法的。这意味着makeChildren
得到它自己的this
,而不是得到你想要的Mother
的this
。
将一个普通变量设置为该值,然后使用它。
var that = this;
function makeChildren(){
blabla = that;
}
不过,我认为这样做还不够。通过从构造函数返回一个对象,可以忽略this
。设置内容:
this.children = children;
而不是返回一个新对象。
当您从母亲对象内部调用makeChildren((时,您可以尝试传递对母亲对象的引用,可能是这样的:
var Mother = function() {
var children = makeChildren(this);
}
然后,makeChildren((函数可以接受引用作为参数,您可以使用它:
function makeChildren(ref)
var c = new Child(ref);
不知道这是否有效,但可能值得一试。
嵌套函数不会从其父函数继承this
,因此makeChildren()
中的this
与Mother构造函数中的this
不同,除非在调用makeChildren()
:时显式设置它
var children = makeChildren.call(this);
这应该可以在不对代码进行任何进一步更改的情况下工作。有关.call((.的更多详细信息,请查看MDN
或者,您可以保存对this
的引用,并将其传递到函数:
var Mother = function() {
var self = this; // <-- new variable
var children = makeChildren();
return {
children: children
}
function makeChildren() {
var children = [];
for (var i = 0; i < 10; i++) {
var c = new Child(self); // <--- change 'this' to 'self'
children.push(c)
}
return children;
}
}
嵌套函数可以访问函数中的局部变量。
var Child = function(m) {
var mother = m;
return {
mother: mother
}
};
var Mother = function() {
if (!(this instanceof Mother)) {
return new Mother();
}
var that = this;
var makeChildren = function() {
var children = [];
for (var i = 0; i < 10; i++) {
var c = new Child(that); // <--- 'that' is the reference to Mother
children.push(c)
}
return children;
};
var children = makeChildren();
return {
children: children
}
};
然后执行:
var m = Mother();
Mother
对象中的前三行确保that
的值是Mother
实例,而不是全局对象。如果没有它们,你将永远不得不写:
var m = new Mother();
相关文章:
- 如何使用object.assign()从其他对象引用基本对象属性
- 返回 JavaScript 类值而不是对象引用
- Facebook:当发布期望对象引用时显示打开的图形对话框
- Javascript-如何从字符串/对象引用回调
- 将其用于对象引用
- 如何将对象引用传递到 mootools 中另一个对象的构造函数中
- Javascript:将类/对象引用设置为 NULL,内存中的子对象/类会发生什么
- 在 Jquery 中,我可以存储对象引用并在以后使用它吗?
- 将对象函数传递给请求动画帧时丢失对象引用
- JavaScript ecma6中的对象引用
- 在JSON中存储对象引用
- 为什么可以在内部函数成员中访问对象引用,而不能在内部属性成员中访问
- 对象引用的隐式传递是如何工作的
- 按对象的 ID 删除对选定对象的对象引用
- 在调用的函数中使用对象引用(this)
- 如何在testcomplete+js中获取对象引用
- 在Angularjs服务中为返回对象引用的http请求提供的数据构建简单的缓存
- JavaScript对象引用
- 对象引用未设置为行中的对象的实例,如果 (!string.IsNullOrEmpty(tdcd.文本)&&
- 如何在使用声明式 Dojo 时获取对象引用