本文介绍了mootools可变范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何访问外部函数的参数"parent"? 请查看代码中的注释!!
最后这个问题有误导作用,我的问题是由错误的输入参数引起的
how to access outer function's argument 'parent' ??? please see comments in code!!
last edit : This question is misleading, my problem is caused by wrong input argument
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));
推荐答案
在每个循环中:
array.each(function(el) {
this.method(); // this == (instance / scope)
}, this); // where **this** is your parent scope.
另一种可接受的方式是:
another acceptable way is:
var self = this;
...
array.each(function(el) {
self.method(); // fine.
}); // where this is your parent scope.
http://mootools.net/docs/core/Types/Array#Array:Array-each
尽管,使用.bind(this)
应该也可以...... http://www.jsfiddle.net /dimitar/fFy4J/-那是什么问题?
although, using .bind(this)
should work too... http://www.jsfiddle.net/dimitar/fFy4J/ - so what is the problem?
这篇关于mootools可变范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!