#TenDaysIsNotEnoughTimeToWriteAProgrammingLanguage
Explore tagged Tumblr posts
scottyschup · 10 years ago
Text
week, day = 5, 4
JavaScript: it's neither a Java, nor a script. Discuss.
Day 1 of JavaScript was a wild success in that I did not want to rip all of my hair out by the end of the day. It's always a little frustrating starting a new language when you've been so throughly immersed in another, but it's also kind of refreshing to think about how to solve familiar problems with a slightly different set of tools at your disposal.
That said, the things that stuck out the most today were the nuanced (and some not-so-nuanced) differences between The Ruby Way and The JavaScript Way. So without further ado...
My desert isalnd, all-time top five Ruby-to-JavaScript annoyances, in the order in which I discovered them:
All numbers in JS are floats. Which is cool at first, until you want to divide an array length by 2 and get a whole number to use as an index, then your life is literally over.
Variable assignment without a preceding var is allowed, but 9 times out of 10 it will break everything. I now know that it has its usefulness, but I wish Brendan had had one more day to work on JS and chosen to make local variable assignment the unmarked form.
JS has no built-in exponent operator. So 2 ** 3 does not equal 8, it equals
SyntaxError: Unexpected token * at Object.exports.createScript (vm.js:44:10) at REPLServer.defaultEval (repl.js:117:23) at bound (domain.js:254:14) at REPLServer.runBound [as eval] (domain.js:267:12) at REPLServer.<anonymous> (repl.js:279:12) at REPLServer.emit (events.js:107:17) at REPLServer.Interface._onLine (readline.js:214:10) at REPLServer.Interface._line (readline.js:553:8) at REPLServer.Interface._ttyWrite (readline.js:830:14) at ReadStream.onkeypress (readline.js:109:10)
The JS Way is to call a method from the Math object like so: Math.pow(base, exp) Which is just fine. I guess.
JS, unlike Ruby (and Python too for that matter), does not allow parallel assignment. This is particularly annoying when you're writing bubble sort and you need to swap two elements in an array. In Ruby/Python, arr[0], arr[1] = arr[1], arr[0] is all you need to get the job done. In JS, it's a little more convoluded.
And finally, Ruby classes and JS classes are Way Differentâ„¢. They're created differently, they behave differently, you monkey-patch them differently, you include and extend them differently...there's just not a lot of overlap there. But syntax aside, they're still classes. They have properties and methods. They serve pretty much the same function (as far as I can tell at this point, at least). And they're hella cool.
So there you have it. A n00b's take on transitioning from Ruby to JS. I'm sure there will be more of the same to come in the next few days. Stay tuned.
P.S. Close runners-up to the top 5: JS requires explicit returns. Also, JS != and Ruby != are not equal. Buh-dum ching!
1 note · View note