View Full Version : How well would this work?

08-11-2007, 01:37 AM
I made a simple function:

const constructorof = function (data) {
return data.constructor.toString ().split ("(") [0].split (" ") [1];
How well would it work?
Also, it works for custom prototypes if the custom constructor was made with a
function thing () {}
var thing = function () {}
doesn't work, is there any way I can make it?

08-11-2007, 02:30 AM
I believe const is not supported by IE. Use var instead.

The constructor of
var thing = function () {} is Function

08-11-2007, 02:59 AM
I also doubt that there is a real use for it anyway. If you hadn't known, you can store the constructor you get through the constructor property into a variable and use it to create objects.

var s = "foo";
var c = s.constructor;
alert(c(false)); //alerts "false"
alert(typeof c(false)); //alerts "string"

08-11-2007, 03:52 AM
It was just a random 5 second idea, but constructor is a function, and it simply getting a string for you.

08-11-2007, 08:13 PM
How well would it work?Nasty hack, but possibly necessary. The best way to do it would be:
function constructorof(o) {
return o.constructor.name;
}... however, some browsers don't support this (IE, I think, included), so your idea's as good as any. It won't, however, work with anonymous functions, since they don't have a name to return (by definition).