0

今日学校でオブジェクトについて学んでいて、オブジェクト内から関数を実行できることは知っていますが、関数は実際にはメソッドと呼ばれ、メソッドには配列があり、特定の宛先をコンソールで起動するのが好きです。グローバル変数を変更してログに記録します。これはできますか?私が得ているのは最後のconsole.logで未定義です-> ing at " + 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);
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 に答える