Week 11 Day 3 – Code Syntax Like WTF?
I’m having a hell of a time figuring out this guy’s code. Let me show you some examples:
"computer" == deviceName ? pageVerticalPosition = "internet explorer" == browserName ? document.documentElement.scrollTop : pageYOffset : (pageVerticalPosition = pageVerticalPositionOnTouch + (touchStartX - touchCurrentX),
This is essentially a ternary nested within a ternary. The first condition checks for the device type, if the device is a computer, it then enters a second conditional. If it isn’t, it sets pageVerticalPosition
to the difference between touchStartX
and touchCurrentX
. When it enters the second conditional, it checks to see whether or not the browser used is Internet Explorer, if it is, it sets pageVerticalPosition
to document.documentElement.scrollTop
, otherwise, pageYOffset
.
After looking at it for awhile, it makes sense, but to have a nested ternary is kinda ridiculous. This shows up on my IDE as one single line… I’m seriously hoping that the code has just been minified.
Here’s some interesting syntax I’ve seen:
if (setLayersMovement(), "horizontal" == layersMovement) {
I’ve never seen syntax like this before. What’s happening here is that setLayersMovement()
is being invoked first, and then the check for layersMovement
is initiated afterwards. I don’t understand why the function couldn’t have been invoked outside of the if conditional as convention dictates, but hey, this works.
One more:
for (; layerHorizontalSpeedArray.length > 0; ) layerHorizontalSpeedArray.pop();
This is another one I’ve never seen before. The for
loop is basically being used as a while
loop. Alongside that, there’s no curly braces… how does that even work?
Anyways, this is extremely slow-going, so I’m trudging along best I can.
Now onto some cool stuff!
Yesterday, I had someone from the App Academy cohort immediately after mine, the March cohort, approach me and ask if I was the one with the blog. This was completely unexpected, but a pretty awesome experience. I asked the student for feedback and asked to see how they were enjoying the blog, and received awesome positive responses.
It’s an awesome feeling to know that I’m able to provide some sort of value to the March cohort, and possibly beyond, so if you see me walking around, and want to say hi, please feel free, I’d love to talk to you!
Otherwise, thanks for reading guys, I’ll talk to you tomorrow!