Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome Console and Javascript Object Type

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.

enter image description here

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:

enter image description here

And here is what I get from creating a constructor function manually:

enter image description here

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?

like image 531
ootoovak Avatar asked Nov 23 '14 03:11

ootoovak


1 Answers

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"
like image 50
WacławJasper Avatar answered Oct 12 '22 05:10

WacławJasper