[英]Get data about the position of our sun through a function. How?
我正在使用mourner / suncalc获取有关太阳的信息。 我想使用SunCalc.getTimes()
获得所有太阳位置的时间,但是为了防止使用“英里长”变量,我想通过一个函数调用每个数据。
我在想这样的事情:
function sun_times(string) {
var sun_times = SunCalc.getTimes(new Date(), 54.528486, -7.569268);
return (sun_times.string.getHours().toString() < 10 ? '0' + sun_times.string.getHours().toString() : sun_times.string.getHours().toString());
}
console.log(sun_times('dawn'));
本示例返回(按预期方式) TypeError: sun_times.string is undefined
。
而不是每次在黎明,黄昏,黄金时段,最低点,航海黎明等时间输入sun_times.dawn.getHours().toString() + ':' + sun_times.dawn.getMinutes().toString()
来代替,我只想输入函数,然后输入要打印的数据,例如sun_times('dawn')
。
我怎样才能最好地做到这一点?
JavaScript对象的工作方式类似于字典结构。 我们使用[]
索引运算符访问仅在运行时已知的属性。 就像数组访问一样,但是使用字符串而不是数字。
function sun_times(string) {
var sun_times = SunCalc.getTimes(new Date(), 54.528486, -7.569268);
return (sun_times[string].getHours().toString() < 10 ? '0' + sun_times[string].getHours().toString() : sun_times[string].getHours().toString());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.