0

I want to know what is considered an object in javascript so that I can answer the simple question of whether the number literal 1 (or any other literal) is an object.

I tried these

typeof 1
> "number"

1 instanceof Number
> false

Number(1) instanceof Number
> false

Number(1) instanceof Object
> false

Number instanceof Object
> true

new Number(1) instanceof Object
> true

new Number(1) instanceof Number
>true

(1).constructor
> ƒ Number() { [native code] }

(1).constructor.constructor
> ƒ Function() { [native code] }

(1).constructor.constructor.constructor
> ƒ Function() { [native code] }

(1).constructor.constructor.constructor.constructor
> ƒ Function() { [native code] }

Function instanceof Object
> true

1 === Number(1)
> true

1 === new Number(1)
> false

(1).constructor
> ƒ Number() { [native code] }

(new Number(1)).constructor
> ƒ Number() { [native code] }

Number.constructor
> ƒ Function() { [native code] }

Function instanceof Object
> true

So... can anyone tell me if this question can even be answered?

How does javascript differentiate between the literal 1 and the new Number(1) if they have the same constructor? Does JS just lie to me that they have the same constructor?

What's an object even? Probably it doesn't matter, but I'm curious whether this has some sort of rule-based answer.

Can anyone guide me through what I'm seeing? Because what makes sense to me is that there are differing object hierarchies in Javascript, and JS is lying to me about the constructors of the literals...

vlad-ardelean
  • 7,480
  • 15
  • 80
  • 124
  • 3
    Check this free book about javascript types: https://github.com/getify/You-Dont-Know-JS/blob/master/types%20&%20grammar/README.md#you-dont-know-js-types--grammar – Hedegare Sep 13 '17 at 13:07
  • I think generally the answer is "it depends." Javascript has both a "native" number type, and a "boxed" Number type, and it can convert between them, presumably using rules about as arcane as those for type coercion. – millimoose Sep 13 '17 at 13:09
  • Isn't this the concept, that numbers or strings which are not explicitly invoked via a constructor are considered scalar types? https://developer.mozilla.org/en-US/docs/Glossary/Primitive – Jankapunkt Sep 13 '17 at 13:10
  • Also, calling a function as a constructor with `new`, and calling a function as a function without `new` can have different effects. Calling `Number()` without `new` just converts the given argument to a **primitive** number. – millimoose Sep 13 '17 at 13:13
  • _"and JS is lying to me about the constructors of the literals..."_... What? Why would JS be lying? What makes you think the issue is on JS's side, and not with your interpretation? – Cerbrus Sep 13 '17 at 13:18
  • 1
    Literals/primitives don't have constructors, but they are boxed implicitly when needed: https://github.com/getify/You-Dont-Know-JS/blob/master/types%20%26%20grammar/ch3.md#boxing-wrappers . Javascript isn't "lying" about their constructors, they don't have any constructors, nor do they have any properties, but any attempt to access a property will box a primitive in its object counterpart. – millimoose Sep 13 '17 at 13:25
  • @Cerbrus My issue, which @millimoose solved was this: How come `(1).constructor` and `(new Number(1)).constructor` say they have the same constructor, BUT then behave differently? It was the automatic boxing that I was not aware of. If the literal `1` was different than the `Number` instance, I would expect their constructors not to differ, but those 2 things were the same under every investigation I could think of because of the automatic boxing. – vlad-ardelean Sep 14 '17 at 08:59

1 Answers1

1

1 is a primitive, because it's a number.

In javascript there are 6 different types or primitives:

  • boolean
  • number
  • string
  • null
  • undefined
  • symbol

Everything else is an object.

Kristianmitk
  • 4,528
  • 5
  • 26
  • 46
Josep
  • 12,926
  • 2
  • 42
  • 45
  • 2
    Actually `symbol` is an Primitive as well! – Kristianmitk Sep 13 '17 at 13:14
  • `typeof null` => 'object' – Hedegare Sep 13 '17 at 13:15
  • `Symbol instanceof Object` returns True.... – vlad-ardelean Sep 13 '17 at 13:15
  • 1
    https://developer.mozilla.org/en-US/docs/Glossary/Primitive "Except for `null` and `undefined`, all primitive values have object equivalents that wrap around the primitive values..." – Kristianmitk Sep 13 '17 at 13:15
  • @vlad-ardelean: As does `Number instanceof Object`. But that does not mean `1` is a object. – Cerbrus Sep 13 '17 at 13:17
  • @govInda you are correct, `symbol` is a primitive too. I just accepted your edit. Thanks! – Josep Sep 13 '17 at 13:21
  • @vlad-ardelean `Symbol instanceof Object` doesn't mean "is every symbol an object?", but "is the `Symbol` value an `Object`." Since `Symbol` is the coercion / constructor function for symbols/Symbols, it returns true, because every function is an object. `instanceof` does not tell you about relationships between types, but between an instance and a type. – millimoose Sep 13 '17 at 13:27