从对象方法内部的数组中运行console.log

Running a console.log from an array thats inside a method of an object

本文关键字:运行 console log 数组 对象 方法 内部      更新时间:2023-09-26

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