Вопрос

In JavaScript, typeof 0 gives 'number' not 'Number', but instanceof 0 Number.

Would it be accurate to say the canonical names of the built-in types are capitalized, and the lowercase return value of typeof is a quirk/inconsistency that can't be changed for historical reasons, but would be changed if it could be? Or am I missing something?

Это было полезно?

Решение

No,

Actually number is the built-in value type where Number is an object.

If you say typeof there's no need to temporarily convert 0 to an Object.

If you use instanceof, it temporarily converts 0 to an object.

This is similar to what you do with a string:

"sometest" => This is a string

However, if you would do "sometest".toLowerCase() it will first (temporarily) convert the string to a String-object and then call the method on that object (since value-types can't have methods).

In short, lowercase means value-type, uppercase means object

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top