[英]inner function variable losing 'this' value of outer function
I created a class in JavaScript as follows: 我使用JavaScript创建了一个类,如下所示:
class TreeMatching
{
constructor()
{
this.thresholdPoints=0;
this.neighborWeight = 0.4;
this.totalFrequency = 0.0;
this.listSeq = [];
this.listFreq = [];
this.mapScore = new Object();
this.tree = new Trie();
}
createTree()
{
var list_Dictionary;
var loadWordList = $.get("../wordFrequencyTop5000.txt", function(data)
{
list_Dictionary = data.split("\n");
});
loadWordList.done(function()
{
for(var i=0;i<list_Dictionary.length;i++)
{
var string = list_Dictionary[i];
this.tree.insert(string); //<-- Cannot read property 'insert' of undefined
}
});
}
}
which is supposed to call the insert
method in class Trie as follows: 它应该按如下所示在Trie类中调用
insert
方法:
class Trie
{
constructor()
{
this.count=1;
this.root = new TrieNode();
}
insert(word)
{
var children = new Object();
for(var i=0; i<word.length(); i++){
var c = word.charAt(i);
var t;
if(children[c]){
t = children[c];
}else{
t = new TrieNode(c);
children.put(c, t);
}
children = t.children;
//set leaf node
if(i==word.length()-1)
t.isLeaf = true;
}
}
}
However, the line of code where the error is marked, the outer function's this value, is not having properties tree
, mapScore
, etc. 但是,标记错误的代码行(外部函数的this值)没有属性
tree
, mapScore
等。
Is there a way that I can access those values from the inner callback function? 有没有办法可以从内部回调函数访问这些值?
Thanks 谢谢
look at 'this' - you will have to define local variable to maintain reference to "this" inside the call, as described in the link. 看一下' this'-您将必须定义局部变量以维护对调用中对“ this”的引用,如链接中所述。
createTree()
{
var self = this;
var list_Dictionary;
var loadWordList = $.get("../wordFrequencyTop5000.txt", function(data)
{
list_Dictionary = data.split("\n");
});
loadWordList.done(function()
{
for(var i=0;i<list_Dictionary.length;i++)
{
var string = list_Dictionary[i];
self.tree.insert(string); //<-- Now you should be able to do it
}
});
}
'this' in the inner anonymous has different scope. 内部匿名中的“ this”具有不同的范围。 Try to use the advantage of closer in JS which will get access to the function caller scope.
尝试利用JS中close的优势,它将可以访问函数调用程序范围。
var that = this;
loadWordList.done(function() {
for(var i=0;i<list_Dictionary.length;i++)
{
var string = list_Dictionary[i];
that.tree.insert(string); // 'that' will hold 'this' in the right scope
}
});
The anonymous function inside loadWordlist.done creates a new scope with an new context. loadWordlist.done内部的匿名函数创建具有新上下文的新范围。
if you want to keep the old context you can use the ES2015 arrow function: 如果要保留旧的上下文,可以使用ES2015箭头功能:
loadWordList.done(() => {
//code here
);
or make a var inside createTree() like this: 或在createTree()中创建一个var,如下所示:
var that = this;
and then inside the loadWordList callback you can refer to the right context using: 然后在loadWordList回调中,您可以使用以下命令引用正确的上下文:
that.tree.insert(string);
I personally prefer the arrow function because 'that' is a lousy choice for a var name. 我个人更喜欢箭头功能,因为“那”是var名称的糟糕选择。 And since your using the ES2015 classes browser support must not be an issue.
并且由于您使用ES2015类浏览器,因此一定不能成为问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.