jolt jolt - 2 years ago 61
Javascript Question

how to return objects interface name/type?

Is there a function or something to get the interface of an object?

Like, for

, how would I retrieve it?

Thanks in advance!

Answer Source

I think there are just one way to do so: fetch a list of all available interfaces, iterate via this list and check your variable value for type using instanceof operator.

This way should work like following:

var element = ... // here your element comes
var interfaces = [HTMLParagraphElement, HTMLDivElement];
var interface = interfaces.filter(function (x) { return element instanceof x; })[0].toString();

Now you get something like function HTMLParagraphElement() { [native code] } in interface variable (if element is <p />). I assume this only works for V8 (Google Chrome). Now you can use regexies to extract interface name.

interface.match(/function (.+)\(\)/)[0]

To get list of all interfaces you can use following (works only for HTML elements):

var interfaces = [];
for (var key in window) {
  if (key.indexOf('HTML') == 0)

I agree with duri's answer, so you don't need to enumerate all interfaces, using of constructor method is more easy way to get prototype function. After that you can use regex I wrote to extract interface name.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download