簡體   English   中英

從對象方法內部的數組中運行console.log

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

我今天只是在學校里學習對象,我知道可以從一個對象內部運行一個函數,但是該函數實際上稱為方法,我的方法有一個數組,我喜歡在控制台中觸發特定的目標。通過更改全局變量來記錄日志。 能做到嗎? 我得到的所有信息在最后一個console.log->處是未定義的,在“ + worker.getLocation.myLocation);上,實際上,我正在嘗試更改全局變量var myLocation = 0以在其中輸出不同的myLocation 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);
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());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM