console.log가 올바른 결과를 인쇄 할 때이 작은 것이 왜 "정의되지 않음"을 반환하는지 알 수 없습니다. 미리 Thx.JavaScript 함수가 정의되지 않은 이유는 무엇입니까
App.Presentation.prototype.getLeaf = function(leafSlug, tree) {
for (var key in tree.content) {
if (tree.content[key].type === 'leaf' && tree.content[key].slug === leafSlug) {
console.log(tree.content[key]) // this works Correct
return tree.content[key]; // this returns undefined :<
} else {
this.getLeaf(leafSlug, tree.content[key]);
}
}
}};
Presentation.getLeaf("chpl", Presentation.tree);
그리고이 결과 얻기 :
I는 다음과 같이 콘솔에서이 전화 드렸습니다
(console.log
에서 첫 번째 결과)
Object {type: "leaf", alias: "Chpl bla bla bla", slug: "chpl", group: "", order: ""…}
alias: "Chpl bla bla bla"
group: ""
html: "<img src='bg.png' />"
order: ""
parent: "chpl"
slug: "chpl"
type: "leaf"
__proto__: Object
(return
에서 다음 결과)
undefined
Presentation.tree
은 객체로 구문 분석 된 JSON을 포함하는 변수입니다.
:
는 또한
undefined
요소를 통해 재귀를 호출하지 않도록 가지의 끝 부분에 대한 테스트를 추가 한? –내 게시물을 업데이트했습니다. 자세한 내용은 해당 게시물을 참조하십시오. – grasnal