在 javascript dojo 闭包中使用的函数使用“this”符号未定义
我在使用“this”访问对象函数时遇到问题。 在下面的示例中(这是简化的,因为由于各种原因我无法提供实际代码),函数调用 this._getEntry() 在调用时是“未定义” 创建列表()。
我希望大家能给出一些意见,看看这是否是由于误解造成的 javascript 闭包或者更确切地说是语法错误。
在后一种情况下,我必须自己找到实际代码中的错误。
如果这是对 javascript 或 dojo 概念的误解,我将非常感激 有关如何正确确定范围和访问下面提到的函数 (_getEntry()) 的一些帮助。
var OBJECT = {
_getEntry : function(entry){
var li = document.createElement('LI');
li.appendChild(document.createTextNode(entry));
return li;
},
createList : function(entryArray){
var list = document.createElement('UL');
dojo.forEach(entryArray,function(entry){
list.appendChild(this._getEntry(entry));
});
dojo.body().appendChild(list);
}
};
OBJECT.createList(["entry1","entry2"]);
谢谢!
I have a Problem accessing an Object function using "this".
In case of the below example (which is simplified, because I cannot supply actual code due to various reasons) the function call this._getEntry() is "undefined" when calling
createList().
I would hope for some opinions on wether this is due to a misunderstanding of
javascript closures or rather a syntax error.
In the latter case I will have to find the error in the actual code myself.
If it is a misunderstanding of javascript or dojo concepts I would really appreciate
some help on how to correctly scope and access the below mentioned function (_getEntry()).
var OBJECT = {
_getEntry : function(entry){
var li = document.createElement('LI');
li.appendChild(document.createTextNode(entry));
return li;
},
createList : function(entryArray){
var list = document.createElement('UL');
dojo.forEach(entryArray,function(entry){
list.appendChild(this._getEntry(entry));
});
dojo.body().appendChild(list);
}
};
OBJECT.createList(["entry1","entry2"]);
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,我认为您粘贴的代码缺少
);
来完成forEach
。其次,
forEach
采用可选的第三个参数,该参数确定传递的函数运行的上下文。如果没有给出,它默认为全局范围,所以是的,这就是你的问题。假设this
已经引用了您需要立即在forEach
之外的内容,您应该能够将this
作为第三个参数传递给forEach
它应该可以工作,例如:有关更多信息:http://dojotoolkit。 org/api/dojo/forEach - 基于 JS 1.6 中的 API https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach
Firstly, I think your pasted code is missing
);
to complete theforEach
.Secondly,
forEach
takes an optional third parameter which determines the context in which the passed function runs. If not given, it defaults to the global scope, so yes this is your problem. Assumingthis
already refers to what you need it to immediately outside theforEach
, you should be able to just passthis
in as the third argument toforEach
and it should work, e.g.:For more information: http://dojotoolkit.org/api/dojo/forEach - which is based on the API in JS 1.6 https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach
这是一个常见的问题,你是对的,这都是关于范围和结束的问题。这里发生的情况是,一旦进入
forEach
,上下文就会更改为全局上下文(window
)。 Dojo 提供了一种方便的方法来设置此上下文:另一种方法是使用闭包来获取
this
This is a common problem and you are right, its all about scoping and closure. What is happening here is once you get into the
forEach
the context changes to the global context (window
). Handily, Dojo provides a way to set this context:An alternate approach is to use closure to get at
this