0

我今天刚刚在学校学习对象,我知道可以从对象内部运行一个函数,但该函数实际上称为方法,我的方法有一个数组,我喜欢在控制台中获取特定的目标。通过更改全局变量来记录。这可以做到吗?我得到的只是在最后一个 console.log -> ing at " + worker.getLocation.myLocation); 处未定义,所以实际上我正在尝试更改全局变量 var myLocation = 0 以输出不同的 myLocation 内部获取位置方法。

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);
4

1 回答 1

1
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());
于 2012-07-13T21:37:13.147 回答