[英]Get data about the position of our sun through a function. How?
I am using mourner/suncalc to get information about our sun. 我正在使用mourner / suncalc获取有关太阳的信息。 I want to get all the times where the position of our sun will be, using
SunCalc.getTimes()
, but to prevent using "miles long" variables, I want to call each data through a function. 我想使用
SunCalc.getTimes()
获得所有太阳位置的时间,但是为了防止使用“英里长”变量,我想通过一个函数调用每个数据。
I am thinking about something like this: 我在想这样的事情:
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'));
This example returns (as expected) TypeError: sun_times.string is undefined
. 本示例返回(按预期方式)
TypeError: sun_times.string is undefined
。
Instead of entering sun_times.dawn.getHours().toString() + ':' + sun_times.dawn.getMinutes().toString()
for every time at dawn, dusk, golden hour, nadir, nautical dawn, and so on, I just want to enter the function and then the data I want to print out, for an example sun_times('dawn')
. 而不是每次在黎明,黄昏,黄金时段,最低点,航海黎明等时间输入
sun_times.dawn.getHours().toString() + ':' + sun_times.dawn.getMinutes().toString()
来代替,我只想输入函数,然后输入要打印的数据,例如sun_times('dawn')
。
How can I accomplish this the best way? 我怎样才能最好地做到这一点?
A JavaScript object works like a dictionary structure. JavaScript对象的工作方式类似于字典结构。 We use the
[]
indexing operator to access properties only known at runtime. 我们使用
[]
索引运算符访问仅在运行时已知的属性。 It's like array access, but with strings instead of numbers. 就像数组访问一样,但是使用字符串而不是数字。
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.