How to know what is the class name of a javascript object?

2

In PHP we can find out from which instance an object comes through the function get_class .

So:

$ao = new ArrayObject;

get_class($ao); // ArrayObject

And in the javascript? How can we do this?

Example:

var f = new FormData();
console.log(/** Qual é o nome da classe de 'f' **/)
    
asked by anonymous 01.10.2015 / 18:46

2 answers

4

Use the name property of the object constructor:

var f = new FormData();
console.log(f.constructor.name)
    
01.10.2015 / 18:49
0

There is also this small form:

var f = new FormData;
Object.prototype.toString.call(f) // [object FormData]

With the idea of @felipsmartins, this can be done:

function get_class(obj)
{
    return obj.constructor.name;
}

get_class('uma String'); // "String"
get_class(1); // "Number"
    
01.10.2015 / 18:52