Section 13.2 of the ECMAScript 5.1 specification explains how all functions have a prototype property which itself has a constructor property.
A prototype property is automatically created for every function, to allow for the possibility that the function will be used as a constructor.
- Let proto be the result of creating a new object as would be constructed by the expression
new Object() where Object is the standard built-in constructor with that name.
- Call the [[DefineOwnProperty]] internal method of proto with arguments
"constructor", Property Descriptor {[[Value]]: F, { [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true}, and false.
So any object created with new someFunction should, by default, inherit the constructor property whose value is the someFunction function. The specification also specifies the constructor property of builtin object prototypes.
However, it does not mention the existence of a name property on constructors or functions. So if you want to specifically get the name of the constructor as a string, you would have to extract it from the representation returned by .toString() which is defined as such:
An implementation-dependent representation of the function is returned. This representation has the syntax of a FunctionDeclaration. Note in particular that the use and placement of white space, line terminators, and semicolons within the representation String is implementation-dependent.
Alternatively, you can use Object.prototype.toString.call(obj) to produce a string which exposes the internal [[Class]] of a value, which is slightly different notion than the constructor property. Again, you'll have to extract the [[Class]] from the string yourself. Sadly, for custom functions, that just returns [object Object], so probably not what you're looking for.
- If the this value is undefined, return
"[object Undefined]".
- If the this value is null, return
"[object Null]".
- Let O be the result of calling ToObject passing the this value as the argument.
- Let class be the value of the [[Class]] internal property of O.
- Return the String value that is the result of concatenating the three Strings
"[object ", class, and "]".
.constructor.name.constructor.toString()? It should befunction <NAME>(...) {..}so you should be able to extract it with some regular expression..constructor.namein ES5-1 and you got an error or something unexpected? Can you clarify?.toString()is implementation-dependent, so...