Skip to content

Commit e65c125

Browse files
committed
The last couple of changes for today
1 parent ef66875 commit e65c125

File tree

6 files changed

+108
-83
lines changed

6 files changed

+108
-83
lines changed

doc/casting.md

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
## Type Casting
1+
## Type casting
22

3-
Since JavaScript is a weakly typed language one might think that it is the best
4-
to let the language to the type casting when comparing. But this is **not** a good
5-
idea, since the rules for converting between the types are extremely complex and
6-
even obscure at times.
7-
8-
### Never use type coercion
9-
10-
Do **never** compare by using the [double equal](#equality) - one little change
11-
in your code can break everything.
3+
JavaScript is a *weakly typed* language, so it will apply *type ceorcion*
4+
wherever possible.
125

136
// These are true
14-
new Number(10) == 10; // Number.toString() gets converted to a Number
7+
new Number(10) == 10; // Number.toString() is converted
8+
// back to a number
9+
1510
10 == '10'; // Strings gets converted to Number
1611
10 == '+10 '; // More string madness
1712
10 == '010'; // And more
@@ -22,9 +17,16 @@ in your code can break everything.
2217
10 == 010;
2318
10 == '-10'
2419

25-
> **Note:** Number literals that start with a `0` are interpreted as octals
20+
> **Note:** Number literals that start with a `0` are interpreted as octal (Base
21+
> 8).
22+
23+
In order to avoid that the use of the [strict equal operator](#equality) is
24+
recommended.
25+
26+
But this does still not solve all the issues that arise from JavaScript's weak
27+
typing system.
2628

27-
### Madness with `new` and built in Types
29+
### Madness with `new` and built in types
2830

2931
The constructors of the built in types like `Number` and `String` behave
3032
differently when being used with the `new` keyword and without it.
@@ -33,33 +35,29 @@ differently when being used with the `new` keyword and without it.
3335
Number(10) === 10; // True, Number and Number
3436
new Number(10) + 0 === 10; // True, due to implicit conversion
3537

36-
As you can see, using the built in type like `Number` as a constructor
37-
will create a new Number `Object`, but leaving out the `new` keyword will make
38-
it behave like a converter.
38+
Using the built in type like `Number` as a constructor will create a new number
39+
`Object`, but leaving out the `new` keyword will make it behave like a converter.
3940

40-
In addition, having literals or non `Object` values in there will activate more coercing
41-
magic.
41+
In addition, having literals or non `Object` values in there will activate more
42+
coercing magic.
4243

43-
Since the automatic casting done either by using different data types together, or the
44-
double equal operator is everything **but** consistent, the best option is to do
45-
the casting explicitly, basically all you can do is to cast to three different
46-
types.
44+
The best option is to do cast to one of the three possible types explicitly.
4745

48-
### Casting to a String
46+
### Casting to a string
4947

5048
'' + 10 === '10'; // true
5149

52-
By using a empty string one can easily cast to a plain string.
50+
By using a empty string a value can easily be casted to a plain string.
5351

54-
### Casting to a Number
52+
### Casting to a number
5553

5654
+'10' === 10; // true
5755

5856
Using the **unary** plus operator it is possible to cast to a plain number.
5957

60-
### Casting to a Boolean
58+
### Casting to a boolean
6159

62-
By using the **not** operator twice, one can convert anything to its boolean
60+
By using the **not** operator twice, a value can be converted to its boolean
6361
value.
6462

6563
!!'foo'; // true

doc/equality.md

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
## Equality and Comparisons
1+
## Equality and comparisons
22

33
JavaScript has two different ways of comparing the values of objects for else
4-
equality. It has both the `==` (double equal) operator and the `===`
5-
(triple equal) operator.
4+
equality.
65

7-
There is an important difference between those two and a good reason for
8-
using **only** the triple equal version
6+
### The equals operator
97

10-
JavaScript has *weak typing* and therefore the creators of the language built in
11-
a way of doing *type coercion* when comparing two values. The only problem is
12-
that, instead of making *type coercion* optional, they made it the **default**.
8+
The equals operator consists of two equal signs: `==`
139

14-
So the `==` operator will try everything that the language specification allows for to
15-
convert the two values to the same type and then compare them.
16-
17-
### The Double Equal Operator
10+
JavaScript features *weak typing*, that means, the equals operator does
11+
**coerce** types in order to compare them.
1812

1913
"" == "0" // false
2014
0 == "" // true
@@ -26,15 +20,20 @@ convert the two values to the same type and then compare them.
2620
null == undefined // true
2721
" \t\r\n" == 0 // true
2822

29-
As you can see from this mess, there's absolutely **no** good reason to use the
30-
`==` operator. All that *type coercion* does is to introduce hard to track
31-
down errors due to implicit conversion of types.
23+
The above table shows the results of the type coercion and it is the main reason
24+
why the use of `==` is regarded as bad practice, it introduces hard to track down
25+
bugs due to its complicated conversion rules.
26+
27+
Additionally there is also a performance impact when type coercion is in play,
28+
since for example a string has to be converted to a number before the comparison
29+
is done.
30+
31+
### The strict equals operator
3232

33-
There's also a performance impact when type coercion is in play. So `==` might
34-
end up being a lot slower, while `===` on the other hand, is always **at least**
35-
as fast, or faster when dealing with different types.
33+
The strict equals operator consists of **three** equal signs: `===`
3634

37-
### The Triple Equal Operator
35+
Other than the normal equals operator, the strict equals operator does **not**
36+
coerce the types of its operands.
3837

3938
"" === "0" // false
4039
0 === "" // false
@@ -46,12 +45,16 @@ as fast, or faster when dealing with different types.
4645
null === undefined // false
4746
" \t\r\n" === 0 // false
4847

49-
These are the results one coming from a strongly typed language would expect.
48+
The above results not only make a lot more sense, they also get rid of most of
49+
the weak typing in the language. This makes writing code a lot easier since
50+
things will break earlier and a lot of subtle bugs can be avoided.
5051

51-
### Comparing Objects
52+
It will also be a lot faster when the operands are of different types.
5253

53-
While both `==` and `===` are stated as equality operators, they behave different
54-
when used with at least one `Object`.
54+
### Comparing objects
55+
56+
While both `==` and `===` are stated as **equality** operators, they behave
57+
different when at least one of their operands happens to be an `Object`.
5558

5659
{} === {}; // false
5760
new String('foo') === 'foo'; // false
@@ -60,11 +63,12 @@ when used with at least one `Object`.
6063
foo === foo; // true
6164

6265
Here both operators compare for **identity** and not equality - that is, they
63-
will compare for the same **instance** of the object, much like `is` in Python and a
64-
pointer comparison in C.
66+
will compare for the same **instance** of the object, much like `is` in Python
67+
and a pointer comparison in C do.
68+
69+
### Best practices
6570

66-
### Best Practices
71+
It is highly recommended to **only** use the strict equals operator. In cases
72+
where types need to be coerced, it should be done [explicitly](#casting) and not
73+
left to the "magic" of the languages complicated coercing rules.
6774

68-
**Always** use the `===` operator. There is never a **any** reason at all to
69-
use `==`. You will avoid a lot of potential - yet again - subtle bugs this way.
70-
In cases where you need to coerce types, do so **explicitly**.

doc/instanceof.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
## The `instanceof` operator
22

33
`instanceof` is only useful when comparing custom made objects. Besides that it
4-
returns a mess similar to the [typeof operator](#the-typeof-operator).
4+
returns a mess similar to the [typeof operator](#typeof).
55

6-
### Comparing custom Objects
6+
### Comparing custom objects
77

88
function Foo() {}
99
function Bar() {}
@@ -20,12 +20,14 @@ returns a mess similar to the [typeof operator](#the-typeof-operator).
2020
'foo' instanceof String; // false
2121
'foo' instanceof Object; // false
2222

23-
The important thing to note here is that `instanceof` will **of course** not work when the
24-
two objects origin from different JavaScript contexts e.g. different documents in
23+
One important thing to note is that `instanceof` does of course not work on
24+
objects that origin from different JavaScript contexts. For example: Different
25+
documents in a web browser.
2526
a Web Browser.
2627

27-
### Best Practices
28+
### Best practices
29+
30+
The `instanceof` operator should only be used when dealing with custom made
31+
objects that origin from the same JavaScript context. Just like the `typeof`
32+
operator, every other use of it should be **avoided**.
2833

29-
Only use `instanceof` when dealing with custom made objects, **never** use it like
30-
the `typeof` operator - it will behave just as badly and even worse when dealing
31-
with objects from different contexts.

doc/objects.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,25 @@ object [inherits](#prototype) from `Object.prototype` and has no
3737
// a new object with a property called 'test' with value 12
3838
var bar = {test: 12};
3939

40+
### Access of properties
41+
42+
The properties of an object can be accessed in two ways. Either via the dot
43+
notation, or the square bracket notation.
44+
45+
var foo = {name: 'Kitten'}
46+
foo.name; // kitten
47+
foo['name']; // kitten
48+
49+
var get = 'name';
50+
foo[get]; // kitten
51+
52+
foo.1234; // SyntaxError
53+
foo['1234']; // works
54+
55+
Both notation are identical in their workings, the only difference being that
56+
the square bracket notation allows for dynamic setting of properties as well as
57+
the use of property names that would otherwise lead to a syntax error.
58+
4059
### Notation of keys
4160

4261
var test = {
@@ -50,5 +69,5 @@ another mis-design in JavaScript's parser, the above raises a `SyntaxError`.
5069
The error is getting raised because `delete` is a *keyword* of the language,
5170
therefore it must be, just like `case`, notated as a string literal.
5271

53-
[1]: http://en.wikipedia.org/wiki/Hashmap
72+
[1]: http://en.wikipedia.org/wiki/Hashmap
5473

doc/semicolon.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
## Automatic Semicolon Insertion
1+
## Automatic semicolon insertion
22

3-
Although JavaScript has C style syntax, it does not enforce the use of
4-
semicolons in the source code, but since the parser still needs them in order to
5-
be able to figure out what the code should do, it inserts them automatically.
3+
Although JavaScript has C style syntax, it does **not** enforce the use of
4+
semicolons in the source code. Since the parser still needs semicolons in order
5+
to be able to figure out what the code should do, it inserts them
6+
**automatically**.
67

7-
When the parser encounters an error due to new line that is not preceded by a
8+
When the parser encounters an error due to newline that is not preceded by a
89
semicolon, it will insert a semicolon automatically and try again. When the
910
parser still hits an error, it will raise it, otherwise it will simply proceed.
1011

11-
This is one of the **biggest** flaws in the language since it makes the below
12-
code work with a completely different result than intended.
12+
The automatic insertion of semicolon is considered to be one of **biggest**
13+
design flaws in the language. It makes the below code work, but with a
14+
completely different result than intended.
1315

1416
return
1517
{
@@ -19,16 +21,16 @@ code work with a completely different result than intended.
1921
After the JavaScript parser fixed it, this will **not** return an object which
2022
has a property called `foo`, it will instead simply return `undefined`.
2123

22-
### How the Parser "fixes" missing Semicolons
24+
### How the parser "fixes" missing semicolons
2325

24-
return // parse error, expected a semicolon. Automatic insertion happens
25-
{ // Although there's no block scope in JS, block syntax is handle fined
26+
return // Error, semicolon expected. Automatic insertion happens
27+
{ // Block syntax is handle just fine
2628

27-
// foo is not interpreted as an object key, but as a label
29+
// foo is not interpreted as property name, but as a label
2830
foo: 1 // JavaScript supports single expression evaluation
2931
// So 1 evaluates to 1 and no error is being raised
3032

31-
} // Another semicolon gets inserted here
33+
} // Automatic semicolon insertioN
3234

3335
After the parser has done its "magic", the resulting code has completely
3436
different behavior.
@@ -40,9 +42,9 @@ different behavior.
4042
foo: 1
4143
};
4244

43-
### Best Practices
45+
### Best practices
4446

45-
**Never** omit semicolons. Also, always keep your `{}` on the **same** line to
46-
avoid such subtle mistakes. It's also good style to **never** make use the
47-
possibility of omitting the curly braces for one line `if / else` statements.
47+
It is **highly** recommended to **never** omit semicolons, it is further
48+
recommended to keep braces on the same line with their associated statements and
49+
never omit them for one line `if` / `else` statements.
4850

doc/undefined.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ anonymous function wrapper and add an additional parameter which one passed
3535

3636
var undefined = 123;
3737
(function(something, foo, undefined) {
38-
// undefined in the local scope does now again refer to the value
39-
// undefined
38+
// undefined in the local scope does
39+
// now again refer to the value
4040

4141
})('Hello World', 42);
4242

0 commit comments

Comments
 (0)