问题描述
- Javascript的对象引用问题。
-
function GiveMovieTime() {
var property;for (property in Movie) { if (property == "Time") { alert("on " + Movie.Time[TimeLength]); } else { alert(Movie[propety]); } }
}
就是这个代码,是for循环语句有问题吗? 在调用的时候没有反应啊。
定义的对象是:
var Movie = {
name: "Star",
type: "Tech",
level: 5,
Time:["1:00pm","2:00pm","3:00pm"]
}
var TimeLength = Movie.Time.length;
解决方案
for 怕是不能这样写吧?
解决方案二:
for (var property in Movie) {// * 这里少了一个var *
if (property == "Time") {//这句话是为了检索到property=time的value值,即
//["1:00pm","2:00pm","3:00pm"]
alert("on " + Movie.Time[TimeLength]);
}
else {
alert(Movie[propety]);
}
}
//////////////
var TimeLength = Movie.Time.length;也不是这样子定义的
应该是:var TimeLength = Movie[Time].length;
解决方案三:
for (property in Movie) {
if (property == "Time") {
alert("on " + Movie.Time[TimeLength-1]);//这里也有问题。。数组下标是从0开始的
}
else {
alert(Movie[property]);//这里变量名报错了。。少了r
}
}
时间: 2024-09-22 11:18:02