我今天刚在学校学习对象,我知道一个函数可以从一个对象内部运行,但这个函数实际上被称为一个方法,我的方法有一个数组,我喜欢通过改变全局变量来在console.log中触发一个特定的目的地。这可以做到吗?我得到的结果是在最后一个console.log -> in“+worker.getLocation.myLocation)时没有定义;所以实际上我正在尝试更改全局变量var myLocation =0,以输出不同的myLocation inside getLocation方法。
var myLocation = 0
var worker = {
realName: "Willson",
title: "Assistant Maintenance Supervisor",
maintenance: true,
vehicals: ["2008 Dodge Caliber SRT-4", "2012 Jeep Wrangler Unlimited" ],
getLocation: function () {
myLocation [0] = "Villas at Sunrise Mountain";
myLocation [1] = "Reno Villas";
myLocation [2] = "lost";
myLocation [3] = "back home";
}
};
console.log(worker.realName + " is a " + worker.title + " and drives a " + worker.vehicals[1] + " to work.");
var destination = {
property: ["Villas at Sunrise Mountain", "Reno Villas", "lost", "back home"]
};
console.log ("He sold his " + worker.vehicals[0] + "." + " Today he is working at " + worker.getLocation.myLocation);发布于 2012-07-14 05:37:13
var myLocation = 0
var worker = {
realName: "Willson",
title: "Assistant Maintenance Supervisor",
maintenance: true,
vehicals: ["2008 Dodge Caliber SRT-4", "2012 Jeep Wrangler Unlimited" ],
getLocation: function () {
var myLocationss = [];
myLocationss [0] = "Villas at Sunrise Mountain";
myLocationss [1] = "Reno Villas";
myLocationss [2] = "lost";
myLocationss [3] = "back home";
return myLocations[myLocation];
}
};
console.log(worker.realName + " is a " + worker.title + " and drives a " + worker.vehicals[1] + " to work.");
var destination = {
property: ["Villas at Sunrise Mountain", "Reno Villas", "lost", "back home"]
};
console.log ("He sold his " + worker.vehicals[0] + "." + " Today he is working at " + worker.getLocation());https://stackoverflow.com/questions/11478693
复制相似问题