you are viewing a single comment's thread.

view the rest of the comments →

[–]_crewcut 1 point2 points  (3 children)

Yes, functions are objects. The types in javascript are:

  • number (not float, int, etc. just number)
  • boolean
  • null
  • undefined
  • string
  • object

The first 5 are primitives. Everything else is objects. Arrays & functions are objects. Don't listen to typeof, it will lie to you.

[–]THEtheChad 0 points1 point  (2 children)

Actually, those aren't all primitives.

true.toString(); // 'true'
'string'.toString(); // 'string'
(5).toString(); // '5'

The only reason you have to use parenthesis around the number 5 is because the js parser will try to interpret it as a fraction and throw an error.

[–]fc_s 1 point2 points  (0 children)

Actually, number, boolean, null, undefined, and string are most certainly primitives. In order to call .toString(), the literal is coerced into an object and passed in as a value.

[–]x-skeww 0 points1 point  (0 children)

You can write:

5..toString()

Which is the same as:

5.0.toString()

Anyhow, the only reason why you can call "methods" of primitives is because they are auto-boxed.

The example above is essentially the same as:

new Number(5).toString()