首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从对象的方法内部的数组运行console.log

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

Stack Overflow用户
提问于 2012-07-14 05:34:01
回答 1查看 77关注 0票数 0

我今天刚在学校学习对象,我知道一个函数可以从一个对象内部运行,但这个函数实际上被称为一个方法,我的方法有一个数组,我喜欢通过改变全局变量来在console.log中触发一个特定的目的地。这可以做到吗?我得到的结果是在最后一个console.log -> in“+worker.getLocation.myLocation)时没有定义;所以实际上我正在尝试更改全局变量var myLocation =0,以输出不同的myLocation inside getLocation方法。

代码语言:javascript
复制
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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-14 05:37:13

代码语言:javascript
复制
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());
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11478693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档