- Joined
- Aug 15, 2012
- Messages
- 593
- Reaction score
- 252
- Primarily Uses
// Let's do some math.true + true; // => 2[] + []; // => ''[] + {}; // => '[object Object]'{} + []; // => 0{} + {}; // => NaN2 / false; // => Infinity/* Also, always remember that both strings and numbers have a binary `+` * operator, and implicit conversions to and from each happen *all the time*. * Really, this can and _will_ cause headaches at some point. */'1' + 1 // => '11''3' * 2 // => 6/* Since we're on the topic of numbers and math, Javascript gives you 53-bit * numbers. Have fun with this: */Math.pow(2, 53) == Math.pow(2, 53) + 1; // => true/* But Javascript is totally consistent -- and don't let anyone tell you * otherwise: */null == 0; // => falsenull > 0; // => falsenull < 0; // => falsenull >= 0; // => truenull <= 0; // => truetypeof NaN // => 'number'typeof null // => 'object'null instanceof Object // => false3 > 2 > 0 // => true3 > 2 > 1 // => false[] == false // => trueif ([]) { console.log('Really?'); } // => 'Really?'if (new Boolean(false)) { console.log('Seriously.');} // => 'Seriously.'// Modulus operations are *completely* inaccurate:-7 % 4; // -3// Let's fix that.Number.prototype.mod = function(n) { return ((this % n) + n) % n;}/* Now we have to do modulus operations in any of the following ways (since we * can't overload the default `%` operator): */(-7).mod(4); // => 1-7 .mod(4); // => 1-7..mod(4); // => 1// Except this one, because it's a syntax error:-7.mod(4);/* Also, division by zero results in infinity, while modulus operations with * zero result in "not a number." In any sane language, both would be errors. * Especially when it comes to _game development_. */2 / 0 // => Infinity2 % 0 // => NaN// Sorting numerical arrays, by default, is nonsense:[1, 300, 5, 10, 2].sort(); // => [ 1, 10, 2, 300, 5 ]// So you have to do this every time instead:[1, 300, 5, 10, 2].sort(function(a, { return a - b;}); // => [ 1, 2, 5, 10, 300 ]/* Sorting using a function like that creates a new function every time you * want something sorted, though, so you might just want to define some utility * function that you can pass into it. Say hello to reinventing the wheel * another 5,000 times. * * Oh, and if you want to actually _compare_ arrays, you'll need to create that * functionality yourself. Here's a ludicrously simple example that doesn't * actually work for anything even remotely complex or unusual: */function arraysEqual(a1, a2) { // Don't use `for (x in y)`-style loops over arrays. They're extremely slow // and greedy. I'm serious. for (var i = 0; i < a1.length; ++i) { if (a1 !== a2) return false; } return true;}/* By the way, you almost always want to declare variables with `var`: if you * don't, they leak into the global scope. Surprise! */function leak(a, { r = a + b; return r;}leak(5, 5);r; // => 10/* At the same time, always declaring variables with `var` can also cause some * really bizarre behavior. Surprise! */var scope = 'global';function timeTravel() { console.log(scope); var scope = 'function';}timeTravel(); // => undefinedYeah, start learning Javascript and its exceedingly bizarre eccentricities. Also, try to avoid using == unless you like the complex, nonsensical logic behind type coercion. Take a look at this: The Abstract Equality Comparison Algorithm. Get back to me when you have that memorized. (Oh, == is actually useful for checking for null and undefined, though, which are two separate things -- so remember to use it for those. And NaN isn't equal to anything, even itself, so check for those with isNaN(object); or perish.)Honestly, I didn't even begin going into the strange behavior of Javascript's variable bindings with closure semantics or how you can even get something as simple as a stack trace in a reliably cross-platform way. (There are third-party libraries that can get you a stack trace, but every one of them has its own assortment of bugs. Irony.)
By the way, firing your game up in a web browser to check it is nice and all, but don't forget that Javascript implementations vary significantly across browsers, and what works in one may either break completely or just leak subtle, hard-to-track bugs into your game in another.
Good luck with MV, everyone. I'm actually being completely serious there -- normally, that would have been sarcastic, but really, I wish you all luck. I'm not touching this version. My only real advice to the scripting community is this: come together, stop reinventing everything, and code defensively. The Javascript library landscape is extremely fragmented because no one can ever agree on the same ways of doing anything; even simple acts like "how to define a class" (prototype, really, but you know what I mean) can be extremely fragmentary (which means it can get tricky trying to determine what an object actually is depending on how it was defined).
Again, come together. All of you. Teach each other, admit your mistakes, and learn from them. Ironically, though, I'm leaving the community after this post, so... I wish you all luck.
Goodbye.
By the way, firing your game up in a web browser to check it is nice and all, but don't forget that Javascript implementations vary significantly across browsers, and what works in one may either break completely or just leak subtle, hard-to-track bugs into your game in another.
Good luck with MV, everyone. I'm actually being completely serious there -- normally, that would have been sarcastic, but really, I wish you all luck. I'm not touching this version. My only real advice to the scripting community is this: come together, stop reinventing everything, and code defensively. The Javascript library landscape is extremely fragmented because no one can ever agree on the same ways of doing anything; even simple acts like "how to define a class" (prototype, really, but you know what I mean) can be extremely fragmentary (which means it can get tricky trying to determine what an object actually is depending on how it was defined).
Again, come together. All of you. Teach each other, admit your mistakes, and learn from them. Ironically, though, I'm leaving the community after this post, so... I wish you all luck.
Goodbye.




