Changeset View
Changeset View
Standalone View
Standalone View
src/docs/flavor/javascript_pitfalls.diviner
@title Javascript Pitfalls | @title JavaScript Pitfalls | ||||
@group javascript | @group javascript | ||||
This document discusses pitfalls and flaws in the Javascript language, and how | This document discusses pitfalls and flaws in the JavaScript language, and how | ||||
to avoid, work around, or at least understand them. | to avoid, work around, or at least understand them. | ||||
= Implicit Semicolons = | = Implicit Semicolons = | ||||
Javascript tries to insert semicolons if you forgot them. This is a pretty | JavaScript tries to insert semicolons if you forgot them. This is a pretty | ||||
horrible idea. Notably, it can mask syntax errors by transforming subexpressions | horrible idea. Notably, it can mask syntax errors by transforming subexpressions | ||||
on their own lines into statements with no effect: | on their own lines into statements with no effect: | ||||
lang=js | lang=js | ||||
string = "Here is a fairly long string that does not fit on one " | string = "Here is a fairly long string that does not fit on one " | ||||
"line. Note that I forgot the string concatenation operators " | "line. Note that I forgot the string concatenation operators " | ||||
"so this will compile and execute with the wrong behavior. "; | "so this will compile and execute with the wrong behavior. "; | ||||
Show All 23 Lines | |||||
You can convert `arguments` to an array using JX.$A() or similar. Note that | You can convert `arguments` to an array using JX.$A() or similar. Note that | ||||
you can pass `arguments` to Function.prototype.apply() without converting it. | you can pass `arguments` to Function.prototype.apply() without converting it. | ||||
= Object, Array, and iteration are needlessly hard = | = Object, Array, and iteration are needlessly hard = | ||||
There is essentially only one reasonable, consistent way to use these primitives | There is essentially only one reasonable, consistent way to use these primitives | ||||
but it is not obvious. Navigate these troubled waters with | but it is not obvious. Navigate these troubled waters with | ||||
@{article:Javascript Object and Array}. | @{article:JavaScript Object and Array}. | ||||
= typeof null == "object" = | = typeof null == "object" = | ||||
This statement is true in Javascript: | This statement is true in JavaScript: | ||||
typeof null == 'object' | typeof null == 'object' | ||||
This is pretty much a bug in the language that can never be fixed now. | This is pretty much a bug in the language that can never be fixed now. | ||||
= Number, String, and Boolean objects = | = Number, String, and Boolean objects = | ||||
Like Java, Javascript has primitive versions of number, string, and boolean, | Like Java, JavaScript has primitive versions of number, string, and boolean, | ||||
and object versions. In Java, there's some argument for this distinction. In | and object versions. In Java, there's some argument for this distinction. In | ||||
Javascript, it's pretty much completely worthless and the behavior of these | JavaScript, it's pretty much completely worthless and the behavior of these | ||||
objects is wrong. String and Boolean in particular are essentially unusable: | objects is wrong. String and Boolean in particular are essentially unusable: | ||||
lang=js | lang=js | ||||
"pancake" == "pancake"; // true | "pancake" == "pancake"; // true | ||||
new String("pancake") == new String("pancake"); // false | new String("pancake") == new String("pancake"); // false | ||||
var b = new Boolean(false); | var b = new Boolean(false); | ||||
b; // Shows 'false' in console. | b; // Shows 'false' in console. | ||||
!b; // ALSO shows 'false' in console. | !b; // ALSO shows 'false' in console. | ||||
!b == b; // So this is true! | !b == b; // So this is true! | ||||
!!b == !b // Negate both sides and it's false! FUCK! | !!b == !b // Negate both sides and it's false! FUCK! | ||||
if (b) { | if (b) { | ||||
// Better fucking believe this will get executed. | // Better fucking believe this will get executed. | ||||
} | } | ||||
There is no advantage to using the object forms (the primitive forms behave like | There is no advantage to using the object forms (the primitive forms behave like | ||||
objects and can have methods and properties, and inherit from Array.prototype, | objects and can have methods and properties, and inherit from Array.prototype, | ||||
Number.prototype, etc.) and their logical behavior is at best absurd and at | Number.prototype, etc.) and their logical behavior is at best absurd and at | ||||
worst strictly wrong. | worst strictly wrong. | ||||
**Never use** `new Number()`, `new String()` or `new Boolean()` unless | **Never use** `new Number()`, `new String()` or `new Boolean()` unless | ||||
your Javascript is God Tier and you are absolutely sure you know what you are | your JavaScript is God Tier and you are absolutely sure you know what you are | ||||
doing. | doing. |
Content licensed under Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) unless otherwise noted; code licensed under Apache 2.0 or other open source licenses. · CC BY-SA 4.0 · Apache 2.0