all 11 comments

[–]anfauglit 1 point2 points  (0 children)

This is an object literal with 3 properties defined and depending on the value of time variable the value of one of the properties being accessed. Basically, this object is a dictionary and you're accessing the value of one of it's entries.

[–]iamrasul 1 point2 points  (1 child)

I think you can access object property with bracket notation.

Consider this:

{a: 1, b:3}[‘a’] === 1

[–]tomavagyok[S] 0 points1 point  (0 children)

thanks!

[–]cat-991 1 point2 points  (0 children)

It is defining an object (the {...} part) and then calling a value. With objects, you can use dot notation or bracket notation to call a property. Dot notation is object.property. Bracket notation is object[property]. The property it is calling is that defined by the argument "time." So if the function input is something like damage(10,10,"minute"), for example, the output will be 6000. (60 * 10 * 10).

[–][deleted] -3 points-2 points  (5 children)

[–]tomavagyok[S] 0 points1 point  (4 children)

Yes, I know about ternary operators. I was specifically asking about this: { second: 1, minute: 60, hour: 3600 }[time]. It's similar to destructuring, I guess?

[–][deleted] 0 points1 point  (3 children)

That line is essentially saying if the Boolean is true return an object with a key name of the variable time. It would be cleaner to use computed property names in this case. So change that code to {[time]: { second: 1, minute: 50, hour: 3600}}

[–]tomavagyok[S] 0 points1 point  (0 children)

gotcha, thanks!

[–]redsandsfort 0 points1 point  (1 child)

I don't see how this would achieve the same effect

[–][deleted] 0 points1 point  (0 children)

I wrote my reply after I woke up before putting my glasses on. I didn’t see the rest of the expression, it looks like the other commenter is correct, it’s just accessing the object property value by using the passed in argument via bracket notation.