@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1@title JavaScript Pitfalls
2@group javascript
3
4This document discusses pitfalls and flaws in the JavaScript language, and how
5to avoid, work around, or at least understand them.
6
7= Implicit Semicolons =
8
9JavaScript tries to insert semicolons if you forgot them. This is a pretty
10horrible idea. Notably, it can mask syntax errors by transforming subexpressions
11on their own lines into statements with no effect:
12
13 lang=js
14 string = "Here is a fairly long string that does not fit on one "
15 "line. Note that I forgot the string concatenation operators "
16 "so this will compile and execute with the wrong behavior. ";
17
18Here's what ECMA262 says about this:
19
20 When, as the program is parsed ..., a token ... is encountered that is not
21 allowed by any production of the grammar, then a semicolon is automatically
22 inserted before the offending token if one or more of the following conditions
23 is true: ...
24
25To protect yourself against this "feature", don't use it. Always explicitly
26insert semicolons after each statement. You should also prefer to break lines in
27places where insertion of a semicolon would not make the unparseable parseable,
28usually after operators.
29
30= `with` is Bad News =
31
32`with` is a pretty bad feature, for this reason among others:
33
34 with (object) {
35 property = 3; // Might be on object, might be on window: who knows.
36 }
37
38Avoid `with`.
39
40= `arguments` is not an Array =
41
42You can convert `arguments` to an array using JX.$A() or similar. Note that
43you can pass `arguments` to Function.prototype.apply() without converting it.
44
45= Object, Array, and iteration are needlessly hard =
46
47There is essentially only one reasonable, consistent way to use these primitives
48but it is not obvious. Navigate these troubled waters with
49@{article:JavaScript Object and Array}.
50
51= typeof null == "object" =
52
53This statement is true in JavaScript:
54
55 typeof null == 'object'
56
57This is pretty much a bug in the language that can never be fixed now.
58
59= Number, String, and Boolean objects =
60
61Like Java, JavaScript has primitive versions of number, string, and boolean,
62and object versions. In Java, there's some argument for this distinction. In
63JavaScript, it's pretty much completely worthless and the behavior of these
64objects is wrong. String and Boolean in particular are essentially unusable:
65
66 lang=js
67 "pancake" == "pancake"; // true
68 new String("pancake") == new String("pancake"); // false
69
70 var b = new Boolean(false);
71 b; // Shows 'false' in console.
72 !b; // ALSO shows 'false' in console.
73 !b == b; // So this is true!
74 !!b == !b // Negate both sides and it's false! FUCK!
75
76 if (b) {
77 // Better fucking believe this will get executed.
78 }
79
80There is no advantage to using the object forms (the primitive forms behave like
81objects and can have methods and properties, and inherit from Array.prototype,
82Number.prototype, etc.) and their logical behavior is at best absurd and at
83worst strictly wrong.
84
85**Never use** `new Number()`, `new String()` or `new Boolean()` unless
86your JavaScript is God Tier and you are absolutely sure you know what you are
87doing.