2013-02-06 3 views
-2

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을 포함하는 변수입니다.

+1

:

는 또한 undefined 요소를 통해 재귀를 호출하지 않도록 가지의 끝 부분에 대한 테스트를 추가 한? –

+0

내 게시물을 업데이트했습니다. 자세한 내용은 해당 게시물을 참조하십시오. – grasnal

답변

1

tree.content이 조건이 참 더 키가없는 경우, 함수 결코 return들 무엇이든 당신은 undefined으로 돌아갑니다.

재귀 호출 중 일부가 log 일지라도 그 결과는 어디에도 사용되지 않습니다. 재귀 호출의 결과를 호출 함수에서 반환해야합니다! 이 해결

var res = this.getLeaf(leafSlug, tree.content[key]); 
if (res) 
    return res; 
+0

내 게시물을 업데이트했습니다. 자세한 내용은 해당 게시물을 참조하십시오. – grasnal

+0

고마워요,하지만 당신은 여전히 ​​기능이해야 할 일과 예상되는 결과를 게시하지 않았습니다. 간단한 나무 검색입니까? – Bergi

+0

이것은 트리 구조에서 재귀 적 검색입니다. 나는 정확한 mach를 가지고 있지만, 정확한 결과를 얻는'console.log'에 의해서만 얻을 수 있습니다. 'return'은 나에게 정의되지 않은 값을줍니다. – grasnal

0

재귀 호출에서 아무 것도 반환하지 않습니다. 이 다른 고려 사항은 여기에 아마, 그러나 어느 시점에서 당신은 재귀 호출의 결과를 반환하기 위해 같은 것을 할 것입니다 :

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 { 
      return this.getLeaf(leafSlug, tree.content[key]);  
     } 
    } 
} 
0

에 다른 분기를 변경합니다. 아무것도 뭔가가 if (tempReturn)을 반환 여부를 확인, 변수 tempReturn = this.getLeaf(leafSlug, tree.content[key]);이 호출의 결과를 할당했다 발견되었을 때

Presentation.prototype.getLeaf = function(leafSlug, tree) { 
    var tempReturn; 
    for (var key in tree.content) { 
     if (tree.content[key].type === 'leaf' && tree.content[key].slug === leafSlug) { 
      return tree.content[key]; 
     } else { 
      if (tree.content[key] instanceof Object && tree.content[key].hasOwnProperty('content')) { 
       tempReturn = this.getLeaf(leafSlug, tree.content[key]); 
       if (tempReturn) { return tempReturn } 
      } 
     } 
    } 
    return false; 
}; 

을 대신 this.getLeaf(leafSlug, tree.content[key]);를 호출하고 경우 예 반환 : 올바른 recurions는 다음과 같이한다 중첩 된 객체의 트리를 통해 검색 그 결과는 { return tempReturn }입니다. 당신이 전화 않으며 당신이 정의되지 않은 반환하는 방법을 어떻게 알 수 있습니까

if (tree.content[key] instanceof Object && tree.content[key].hasOwnProperty('content')) {...} 
관련 문제