Slide 1

Slide 1 text

WHY IS NULL AN OBJECT? Michal Skuza

Slide 2

Slide 2 text

JAVASCRIPT TYPE SYSTEM - QUIRKS ➤ 1 + 1 ➤ 1 + '1' ➤ {} + [] ➤ [] + {} ➤ {} - [] ➤ [] - {} ➤ ({} + []) === 0

Slide 3

Slide 3 text

JAVASCRIPT TYPE SYSTEM - QUIRKS ➤ 1 + 1 // 2 ➤ 1 + '1' // '11' ➤ {} + [] //0 ➤ [] + {} // '[object Object]' ➤ {} - [] // -0 ➤ [] - {} // NaN ➤ ({} + []) === 0 // false ➤ // sigh

Slide 4

Slide 4 text

NULL - TYPE SYSTEM ➤ null is not undefined ➤ null == undefined // true ➤ null === undefined // false // oh ➤ typeof null // 'object' // gosh, why?

Slide 5

Slide 5 text

TYPE SYSTEM - ORIGINAL IMPLEMENTATION ➤ type tag 1-3 bit, values stored in 32 bit units ➤ 5 type tags 1. 000: object. The data is a reference to an object. 2. 1: int. The data is a 31 bit signed integer. 3. 010: double. The data is a reference to a double floating point number. 4. 100: string. The data is a reference to a string. 5. 110: boolean. The data is a boolean.

Slide 6

Slide 6 text

WHY? Two possible answers ➤ It might be a bug ➤ It was intended

Slide 7

Slide 7 text

WHY IS IT NOT FIXED YET? ➤ typeof null === “null”; // true ➤ there was a proposal ➤ but it was rejected ➤ „I think it is too late to fix typeof. The change proposed for typeof null will break existing code.” Douglas Crockford

Slide 8

Slide 8 text

CONCLUSIONS ➤ It can be tricky for people learning JavaScript ➤ Some decision can have a serious impact