日期:2014-05-16  浏览次数:20377 次

关于 for 语句的问题
var   a   =   new   Array();
function   CX(){
this.GetA   =   function(){
return   "cc ";
}
}
a.push(new   CX());
a.push(new   CX());
a.push(new   CX());


for(   b   in   a){
alert(   b.GetA()   );
}

好像这样不行?
for   in   这种语句不支持对象数组么?
如果我一定要用   for   in   的写法,应该怎样做呢?  
thx   thx

------解决方案--------------------
var a = new Array();
function CX(){
this.GetA = function(){
return "cc ";
}
}
a.push(new CX());
a.push(new CX());
a.push(new CX());

var b = new CX();
for( b in a){
alert( a[b].GetA() );
}
------解决方案--------------------
var a = new Array();
function CX(){
this.GetA = function(){
return "cc ";
}
}
a.push(new CX());
a.push(new CX());
a.push(new CX());

for( b in a){
alert(eval( "a[ "+b+ "].GetA(); "));
}
------解决方案--------------------
http://zhidao.baidu.com/question/18438782.html
------解决方案--------------------
http://zhidao.baidu.com/question/18438782.html
------解决方案--------------------
楼主你那个b没有声明吧
------解决方案--------------------
foreach (b in a)
------解决方案--------------------
<script language= "javascript ">
function CX(){
this.GetA = function(){
return "cc ";
}
}
function aa()
{
debugger
var a = new Array();

a.push(new CX());
a.push(new CX());
a.push(new CX());


for(var b in a)//将执行循环0到a.length-1
{
alert( a[b].GetA() );//此时b是a的本次循环的索引值
}
}
</script>
------解决方案--------------------
这里的b是a的一个下标
就要用a[b]来获取a中的一个对象
当然你不var b的话,b就是全局的了