P
Peter Michaux
Hi,
I want to know the name of an object's constructor function as a
string. Something like this
<script type="text/javascript">
function Foo(){};
var a = new Foo();
alert('"' +
a.constructor.toString().match(/function\s*([a-zA-Z\$][\w\$]+)[^\w\$]/)[1]
+ '"');
</script>
Although this works in IE, Opera, Firefox, Safari I'm not sure that it
is guaranteed to work.
I looked in the ECMA-262 specs about the returned string for
Function.prototype.toString() in section 15.2.4.2. It says that the
whitespace, line terminators and semi-colon use are implementation
dependent. I think the above regular expression can handle those
difference.
What I'm worried about is that he specs say the return value has the
form of a "function declaration". Does that mean the returned string
will definitely contain the name of the constructor function? Could it
instead return something like just "function () {}"?
Thank you,
Peter
I want to know the name of an object's constructor function as a
string. Something like this
<script type="text/javascript">
function Foo(){};
var a = new Foo();
alert('"' +
a.constructor.toString().match(/function\s*([a-zA-Z\$][\w\$]+)[^\w\$]/)[1]
+ '"');
</script>
Although this works in IE, Opera, Firefox, Safari I'm not sure that it
is guaranteed to work.
I looked in the ECMA-262 specs about the returned string for
Function.prototype.toString() in section 15.2.4.2. It says that the
whitespace, line terminators and semi-colon use are implementation
dependent. I think the above regular expression can handle those
difference.
What I'm worried about is that he specs say the return value has the
form of a "function declaration". Does that mean the returned string
will definitely contain the name of the constructor function? Could it
instead return something like just "function () {}"?
Thank you,
Peter