Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why javascript typeof returns always "object"

Where am I doing wrong?

I would wait "Class" as a result of this code but it doesn't:

enter image description here

This is from object function:

enter image description here

like image 776
uzay95 Avatar asked Sep 12 '25 19:09

uzay95


2 Answers

Tyepof doesnt work like that, it only returns built in types. You could try:

this.constructor.name==="Class";

It will check all the way up the prototype chain to see if this or any prototype of this is Class. So if OtherType.prototype=Object.create(Class); then it'll be true for any OtherType instances. Does NOT work in < IE9

or

this instanceof Class

But that will not check the entire prototype chain.

Here is a list of return values typeof can return

Here is an answer about getting the type of a variable that has much more detail and shows many ways it can break.

like image 67
HMR Avatar answered Sep 15 '25 07:09

HMR


Because JavaScript knows only the following types :

Undefined - "undefined"

Null - "object"

Boolean - "boolean"

Number - "number"

String - "string"

Host object (provided by the JS environment) - Implementation-dependent

Function object (implements [[Call]] in ECMA-262 terms) - "function"

E4X XML object - "xml"

E4X XMLList object - "xml"

Any other object - "object"

You can find more here

Read this thread to find how you can get the object name

like image 30
LZR Avatar answered Sep 15 '25 08:09

LZR