2011-01-25 118 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