View Full Version : How well would this work?
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 () {}
but
var thing = function () {}
doesn't work, is there any way I can make it?
Trinithis
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
Trinithis
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"
It was just a random 5 second idea, but constructor is a function, and it simply getting a string for you.
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).
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.