0

I have scanned through related questions here but couldn't find a satisfactory answer on how to determine the type of a JavaScript object.

In fact, the Object.prototype.toString.call(obj) technique used below is something I got from one of the threads here. However, it seems not to do the job.

Scenario 1:

var Person = function(name)
{
    this.name = name;
};

var joe = new Person("Joe Bloggs");

console.log(typeof joe); // prints object

console.log(Object.prototype.toString.call(joe)); // still prints [object Object]

Question: How do I get it to print Person?


Scenario 2:

var Person = function(...) { };
var Cat = function() { };

var joe = new Person(...);
var polly = new Cat(...);

Question: How do I distinguish between the two objects based on their types? console.log(typeof obj) or console.log(Object.prototype.toString.call(obj)) on either of them will yield the same result.

instanceof

I am aware of instanceof but that's not a swiss army knife. It is tedious in that it can only be applied for testing whether or not an object is of a certain type. How do I get to know the type of an object?

Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
  • You can use [instanceof](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof) or the `constructor` property directly. Other than that, i don't think javascript is typed well enough to meet your expectations... – ASDFGerte Oct 29 '16 at 10:37
  • 1
    There’s also `joe && joe.constructor` to get the constructor function. And if you declared your function like `function Person(personName){`…`}`, you can also use `joe && joe.constructor && joe.constructor.name` to get `"Person"`. – Sebastian Simon Oct 29 '16 at 10:38
  • @ASDFGerte Yes, I am aware of that. In fact, I have another related question on `instanceof` which I will soon be posting. But that isn't a swiss knife. – Water Cooler v2 Oct 29 '16 at 10:39
  • 2
    Those objects have the same type, `object`. – melpomene Oct 29 '16 at 10:45
  • Why do you want to know the type? It's not usually the right thing to do. – Software Engineer Oct 29 '16 at 10:45
  • @melpomene Be that as it may, it is pertinent to know what ctor they came from, nevertheless. – Water Cooler v2 Oct 29 '16 at 10:45
  • @EngineerDollery: I don't know as of now. I am just learning JavaScript and following my intuition. Coming from a strongly-typed / statically typed language, it just feels intuitive to want to know. – Water Cooler v2 Oct 29 '16 at 10:46
  • You can get the constructor with `obj.constructor`. – melpomene Oct 29 '16 at 10:46
  • There are several typed languages that compile into javascript, e.g. [typescript](http://www.typescriptlang.org/), but even there you only have static type-checking from the compiler. As it emits javascript in the end, the same issues arise when you want to confirm something run-time. It still helps a lot though imho. – ASDFGerte Oct 29 '16 at 10:49
  • 2
    'not strongly typed' basically means that type information is lost at runtime, leaving behind just objects and a handful of primitives. You can't really use javascript as if it were java -- the patterns are different. To a large extent, modern javascript is primarily functional anyway. I think you're not going to find a way to do this satisfactorily. – Software Engineer Oct 29 '16 at 10:49
  • Thank you both, Engineer Dollery and ASDF Gerte. I understand your sentiments and agree with them. I also am aware of TypeScript, ScriptSharp and a few others (SomethingBridge), Babel, CoffeeScript and a whole lot of transpilers. I just want to learn JavaScript for once. – Water Cooler v2 Oct 29 '16 at 10:53
  • Thank you, @Xufox and melpomene. That kind of helps a bit. I am not sure how to then check for object type equality on the basis of what that outputs. However, as indicated by others' comments, perhaps type equality has little significance in a dynamically typed language such as JavaScript. – Water Cooler v2 Oct 29 '16 at 11:03
  • @Xufox tested a while ago in Chrome and FF - Chrome will also set the name "Person" of the constructor if you assign nameless function to a variable (both function Person() {} and var Person = function() {} work in Chrome) – Kondziutek Oct 29 '16 at 11:03
  • 1
    Excellent info. on typeof https://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/ – Vinay Oct 29 '16 at 14:29
  • @Novice Thanks for sharing. Seems like a long read. Will read it tomorrow for sure. – Water Cooler v2 Oct 29 '16 at 17:55

0 Answers0