Javascript check custom object type
How can I check if my javascript object is of a certain type. Show
In the case above asked Sep 28, 2009 at 10:12
Yes, using
answered Sep 28, 2009 at 10:14
T.J. CrowderT.J. Crowder 983k181 gold badges1829 silver badges1803 bronze badges 3 Whatever you do, avoid obj.constructor.name or any string version of the constructor. That works great until you uglify/minify your code, then it all breaks since the constructor gets renamed to something obscure (ex: 'n') and your code will still do this and never match:
Note:
(BTW, I need higher reputation to comment on the other worthy answers here) answered Jul 24, 2016 at 1:39
AAronAAron 3942 silver badges11 bronze badges 1 Idea stolen from http://phpjs.org/functions/get_class/, posted by SeanJA. Ripped down to work with objects only and without need for a regular expression:
I just learned an easier way to extract the function name from the constructor:
answered Apr 1, 2015 at 16:19
kungfoomankungfooman 4,1161 gold badge40 silver badges30 bronze badges 2 While instanceof is a correct answer it sure is ugly syntax. I offer that if you are creating custom objects you can add your own property for type and check against that like so...
This would create an immutable property called type that lives with the object. If you were using the Class syntax you could make it static as well. ... somewhere later ...
... I think this solution is easy to implement, more intuitive, and thus easier for others to use and maintain. answered Apr 4, 2019 at 17:40
The An expression representing the object or
primitive whose type is to be returned. The following table summarizes the possible return values of This list of values is exhaustive. No spec-compliant engines are reported to produce (or had historically produced) values other than those listed. The old Internet Explorer was the only browser known to implement
additional return values, before the spec removed the behavior of typeof null
In the first implementation of
JavaScript, JavaScript values were represented as a type tag and a value. The type tag for objects was A fix was proposed for ECMAScript (via an opt-in), but
was rejected. It would have resulted in Using new operatorAll
constructor functions called with
Need for parentheses in syntaxThe
Interaction with undeclared and uninitialized variables
However, using
Exceptional behavior of document.allAll current browsers expose a non-standard host object
Although Custom method that gets a more specific type
For greater specificity in checking types, here we present a custom
For checking potentially non-existent variables that would otherwise throw a Specifications
Browser compatibilityBCD tables only load in the browser See alsoHow do I know the type of an object?Use the typeof operator to get the type of an object or variable in JavaScript. The typeof operator also returns the object type created with the "new" keyword. As you can see in the above example, the typeof operator returns different types for a literal string and a string object.
How do I know my TS type?Use the typeof operator to check the type of a variable in TypeScript, e.g. if (typeof myVar === 'string') {} . The typeof operator returns a string that indicates the type of the value and can be used as a type guard in TypeScript.
How do you check what type a variable is in JavaScript?typeof is a JavaScript keyword that will return the type of a variable when you call it. You can use this to validate function parameters or check if variables are defined. There are other uses as well. The typeof operator is useful because it is an easy way to check the type of a variable in your code.
How do you check if it is an object in JavaScript?Method 1: Using the typeof operator
The typeof operator returns the type of the variable on which it is called as a string. The return string for any object that does not exist is “undefined”. This can be used to check if an object exists or not, as a non-existing object will always return “undefined”.
|