2011-01-25 6 views
0

외부 함수의 인수 'parent'에 액세스하는 방법 ??? 코드 에있는 의견을 참조하십시오 !!
최종 편집 :가변 범위 mootools

array.each(function(el) { 
    this.method(); // this == (instance/scope) 
}, this); // where **this** is your parent scope. 

다른 허용 방법은 다음과 같습니다 :이 질문은 내 문제가 각 루프 내 잘못 입력 인수

renderData : function(parent, children){ 
      children.each(function(e, index){ 
       var li = new Element('li'); 
       var hasChildren = false; 
       if(e.children && e.children.length >0){ 
        var img = new Element('img'); 
        img.src = 'a1.png'; 
        img.inject(li); 
        hasChildren = true; 
       } 
       if(e.icon){ 
        var img = new Element('img'); 
        img.src = e.icon; 
        img.inject(li); 
       }else{ 
        var img = new Element('img'); 
        img.src = 'b1.png'; 
        img.inject(li); 
       } 
       li.set('html',e.text); 
       console.log(this); 
        // how to access outer function's argument 'parent' ??? 
       li.inject(parent); 
       if(hasChildren){ 
        var ul = new Element('ul'); 
        this.renderData(ul, e.childRen); 
        ul.inject(e); 
       } 
      }.bind(this)); 

답변

1

, 문제는 li.inject(parent)

renderData()

내가 '함수에 매개 변수로 전달 된 이후 당신은'부모 '에 액세스 할 수없는 이유가 없다 할 질수 있다는 것입니다 이 simple test

var test; 
window.addEvent('domready', function(){ 
     test = new TestClass(); 
}); 

var TestClass = new Class({ 
    Implements: [Options, Events], 
    initialize: function(){ 
     this.renderData($('parent'),$$('span')) 
    }, 

    renderData : function(parent, children){ 
      children.each(function(e, index){ 
       console.log(parent); 
      }.bind(this)); 
    } 
}); 

을 시도하고 그것을 잘 작동합니다 ...하지만 난 당신의 코드에 문제 야 무슨 정말 확신하지했습니다

+0

다른 문제로 인해서 죄송합니다. – kuangfuking