Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is typeof Object,String,Number... -a function in Javascript

Below is excerpt from Google Developer Console

typeof Object  // type of Object is function (most confusing part).
"function"      //Same fot all build-in types

Object.constructor 
function Function(){[native code]}// Why not function Object()?

Object.hasOwnProperty("create")  // Here, it is Object since it has property,not typeof      
"true"                              function


dir(Object) // Again, Object is object,it has property (method)
function Object() { [native code] }

Why typeof Object is not Object ? And why Object.constructor is not a function Object() ?

Thank you MIro

like image 847
Miroslav Trninic Avatar asked Jan 19 '26 23:01

Miroslav Trninic


1 Answers

The identifiers Object, String, etc are not "class names" as you might have seen in other languages. Nor are they instances of the specific type.

Object on its own is the constructor for an "Object", i.e. a reference to a function.

To further complicate matters, Javascript functions are also objects, which may have properties. Those properties are often used to add methods to objects.

like image 72
Alnitak Avatar answered Jan 22 '26 12:01

Alnitak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!