将变量传递给一个数组中对象的函数
pass the variable to a function for objects in one array
我有一个名为ary的数组和这个数组中的一些对象,它们是ary[0]、ary[1]、ary[2]、ary[3]和ary[4]。有一个文本属性 在每个 element.I 中想要为 ary 中的所有元素添加一个 eventListener 并首先将 属性 传递给 function.At,我这样做如下:
ary[0].addEventListener(MouseEvent.CLICK,function(e:MouseEvent){toGo(e,ary[0].topname.text)});
ary[1].addEventListener(MouseEvent.CLICK,function(e:MouseEvent){toGo(e,ary[1].topname.text)});
ary[2].addEventListener(MouseEvent.CLICK,function(e:MouseEvent){toGo(e,ary[2].topname.text)});
ary[3].addEventListener(MouseEvent.CLICK,function(e:MouseEvent){toGo(e,ary[3].topname.text)});
ary[4].addEventListener(MouseEvent.CLICK,function(e:MouseEvent){toGo(e,ary[4].topname.text)});
function toGo(e:MouseEvent,str:String){
......
}
</p>
<p>它确实 work.But 当我在 for(...){...} 中更改它时,它有一个错误。</p>
<p><code>for(var i=0;i<arylength;i++){
ary[i].addEventListener(MouseEvent.CLICK,function(e:MouseEvent){toGo(e,ary[i].topname.text)});
}
上面的代码,我报错"TypeError: Error #1010: A term is undefined and has no properties."。那我也换个方式试试。
for(var i=0;i
没有报错,但是我得到的变量"namestr"始终是ary中最后一个元素的变量。为什么?
我哪里错了?
谢谢。
你的第一个 for 循环,错误是 ary 和 length 之间缺少句点。你有 arylength
但它应该是 ary.length
.
执行此操作的更好方法如下:(不使用匿名函数,使用事件的 currentTarget 属性 确定单击了哪个项目)
for(var i=0; i < ary.length; i++){
ary[i].addEventListener(MouseEvent.CLICK,itemClick,false,0,true);
}
function itemClick(e:Event):void {
toGo(e, Object(e.currentTarget).topname.text;
//replace the object cast with whatever type your ary items are
}
//or even better, just go right to the toGo function and figure out the item clicked there.
我有一个名为ary的数组和这个数组中的一些对象,它们是ary[0]、ary[1]、ary[2]、ary[3]和ary[4]。有一个文本属性 在每个 element.I 中想要为 ary 中的所有元素添加一个 eventListener 并首先将 属性 传递给 function.At,我这样做如下:
ary[0].addEventListener(MouseEvent.CLICK,function(e:MouseEvent){toGo(e,ary[0].topname.text)});
ary[1].addEventListener(MouseEvent.CLICK,function(e:MouseEvent){toGo(e,ary[1].topname.text)});
ary[2].addEventListener(MouseEvent.CLICK,function(e:MouseEvent){toGo(e,ary[2].topname.text)});
ary[3].addEventListener(MouseEvent.CLICK,function(e:MouseEvent){toGo(e,ary[3].topname.text)});
ary[4].addEventListener(MouseEvent.CLICK,function(e:MouseEvent){toGo(e,ary[4].topname.text)});
function toGo(e:MouseEvent,str:String){
......
}
</p>
<p>它确实 work.But 当我在 for(...){...} 中更改它时,它有一个错误。</p>
<p><code>for(var i=0;i<arylength;i++){
ary[i].addEventListener(MouseEvent.CLICK,function(e:MouseEvent){toGo(e,ary[i].topname.text)});
}
上面的代码,我报错"TypeError: Error #1010: A term is undefined and has no properties."。那我也换个方式试试。
for(var i=0;i
没有报错,但是我得到的变量"namestr"始终是ary中最后一个元素的变量。为什么?
我哪里错了?
谢谢。
你的第一个 for 循环,错误是 ary 和 length 之间缺少句点。你有 arylength
但它应该是 ary.length
.
执行此操作的更好方法如下:(不使用匿名函数,使用事件的 currentTarget 属性 确定单击了哪个项目)
for(var i=0; i < ary.length; i++){
ary[i].addEventListener(MouseEvent.CLICK,itemClick,false,0,true);
}
function itemClick(e:Event):void {
toGo(e, Object(e.currentTarget).topname.text;
//replace the object cast with whatever type your ary items are
}
//or even better, just go right to the toGo function and figure out the item clicked there.