2011-01-25 167 views
0

如何訪問外部函數的參數'parent'? 請參閱代碼 中的意見!
最後編輯:這個問題是一種誤導,我的問題是由錯誤的輸入參數mootools變量範圍

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 2011-01-26 01:17:30