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.
The question might actually be paraphrased as:
"Do JavaScript (ECMAScript) language conventions apply to and qualify other components of the browser such as the programming objects that interface the DOM?"
The original question uses objects that are supposedly of type Function and used as constructors. The examples shows a dichotomy exists with the programming environment and the DOM interface in how they are represented.
If there is this actual dichotomy, is it made explicit?
This may be the actual issue. If so, the original question should be preceded by this to direct attention to the real problem.
references:
ECMAScript language constructor
details:
First:
Objects ARE functions
No, the are not:
> a = function() {}
function () {}
> a instanceof Object
true
> b = {}
Object
> b instanceof Function
false
The toString
method (which is what gets called when you do string concatenation) is not a reliable way to get information about an object. If I use typeof
, I get the following:
using browser environment:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:5.0.1) Gecko/20100101 Firefox/5.0.1
function
function
function
object
function
function
function
So you see, most of them, apart form Storage
, are actually functions (why it does not work for Storage
, I don't know).
Also keep in mind that the DOM interface can behave differently than native JavaScript objects.
On the other hand, in Chrome the toString
method gives this:
[object Function]
[object Function]
[object Function]
function Storage() { [native code] }
function XMLHttpRequest() { [native code] }
function Worker() { [native code] }
function FileReader() { [native code] }