I would like to find the type of a Javascipt object (the name of the constructor function) but I keep just getting a generic "Object" back with all methods I have tried. I have searched online and no Javascript method I have found has work for me yet. It always just returns the object constructor type as a generic "Object". Yet there it is, staring right at me when I inspect it in the Chrome console. Any ideas how I might get that information in JS?
PS. I am using Browserify's require to separate my code if that make a difference.
The reason I thought it might be because of how Browserify loads require
code is because this is the output I get from a Browserify loaded constructor function:
And here is what I get from creating a constructor function manually:
Regardless of that and regardless of how I create my constructor function var Prey = function() {}
vs var Prey = function Prey() {}
the Chrome dev tools still seem to know what the name of the constructor function was even when Javascript does not seem to know. Perhaps this is because they can inspect the code in the virtual machine in a way the the Javascript language does not have access to but I am wondering if I am missing something. Does anyone know of another way to determine an instance type in Javascript?
The Prey you are seeing is the Prey when you do function Prey(){};
, thus, the function name is Prey. If you define Prey as var Prey = function(){};
then the function name is "";
So basically if you want use the function name, you must give your constructor a name. var Prey = function Prey(){};
EDIT: or its possible that you did a variation of this:
var A = function NAMED(){};
var B = function(){};
B.prototype = Object.create(A.prototype);
var o = new B();
> o
< B {};
> o.constructor.name
< "NAMED"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With