[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 @title Javascript Pitfalls 2 @group javascript 3 4 This document discusses pitfalls and flaws in the Javascript language, and how 5 to avoid, work around, or at least understand them. 6 7 = Implicit Semicolons = 8 9 Javascript tries to insert semicolons if you forgot them. This is a pretty 10 horrible idea. Notably, it can mask syntax errors by transforming subexpressions 11 on 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 18 Here'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 25 To protect yourself against this "feature", don't use it. Always explicitly 26 insert semicolons after each statement. You should also prefer to break lines in 27 places where insertion of a semicolon would not make the unparseable parseable, 28 usually 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 38 Avoid ##with##. 39 40 = ##arguments## is not an Array = 41 42 You can convert ##arguments## to an array using JX.$A() or similar. Note that 43 you can pass ##arguments## to Function.prototype.apply() without converting it. 44 45 = Object, Array, and iteration are needlessly hard = 46 47 There is essentially only one reasonable, consistent way to use these primitives 48 but it is not obvious. Navigate these troubled waters with 49 @{article:Javascript Object and Array}. 50 51 = typeof null == "object" = 52 53 This statement is true in Javascript: 54 55 typeof null == 'object' 56 57 This is pretty much a bug in the language that can never be fixed now. 58 59 = Number, String, and Boolean objects = 60 61 Like Java, Javascript has primitive versions of number, string, and boolean, 62 and object versions. In Java, there's some argument for this distinction. In 63 Javascript, it's pretty much completely worthless and the behavior of these 64 objects 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 80 There is no advantage to using the object forms (the primitive forms behave like 81 objects and can have methods and properties, and inherit from Array.prototype, 82 Number.prototype, etc.) and their logical behavior is at best absurd and at 83 worst strictly wrong. 84 85 **Never use** ##new Number()##, ##new String()## or ##new Boolean()## unless 86 your Javascript is God Tier and you are absolutely sure you know what you are 87 doing.
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |