Clarification:
\"JavaScript constructor\" should be more properly be written as \"javascript constructor\" to emphasize that the constructors considered are not just the nat
When you alert those values the browser engine is alerting value.toString()
so we are talking about why does Function.prototype.toString
behave in a strange manner.
The ES5.1 specification states :
15.3.4.2 Function.prototype.toString ( ) 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.
The toString function is not generic; it throws a TypeError exception if its this value is not a Function object. Therefore, it cannot be transferred to other kinds of objects for use as a method.
Clearly ES5 states that toString
returns an implementation specific string.
If you read the ES Harmony proposals page it states :
function to string – greater specification for problematic Function.prototype.toString (markm, allen)
Here are some more resources :
Basically it's a known issue that toString
on function objects (and especially host objects that are also functions) is undefined behaviour. The TC39 committee is already working on standardizing this.
As you can see the host objects are proposed to be standardized in strawman so it's in the air whether that makes it into ES6. However function objects living in ECMA land should have a standardized toString
method in ES6 as defined on the harmony proposals page.