I was just learning about objects in school today and i know a function can be ran from inside an object, but the function is actually called a method, my method has an array and i like to get a particular destination to fire in the console.log by changing the global variable. Can this be done? All I’m getting is undefined at the last console.log -> ing at ” + worker.getLocation.myLocation); So in all reality I’m trying to change the global variable, var myLocation = 0 to output the different myLocation inside getLocation method.
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);
1 Answer