1
我在写一个递归地从JSON对象构建菜单树的元素。然而,当函数调用本身我得到的错误:this.buildMenu is not a function
递归函数调用聚合物
这里的buildMenu
buildMenu: function(items) {
var node = new Array();
items.forEach(function(elem,index,arr) {
node.push(elem.Name);
if (elem.SubBranch.length > 0) {
return this.buildMenu(elem.SubBranch); //error is here
}
});
return node;
}
调用buildMenu最初的方法
handleRes: function() {
this.response = this.$.categoryList.lastResponse;
this.menuItems = this.buildMenu(this.response);
//...
}
我验证过的数据是存在的,正确格式化。如果我注释掉递归调用,我会得到第一层结果。所以,它在这方面工作。
在递归调用中调用的参数elem.SubBranch
是一个数组,并且完全有效,如果这很重要的话。
你可以建立一个蹲点来证明问题吗? –