Function, Object, Status.

Recently we found that Firefox has changed the type of object of a function. Below is the test code:

<DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>FX function status test</title>
</head>
<body>
    <script type="text/javascript">
        function hello() {
            this.hello = "hello";
            this.world = "world";
            this.status = "status";
 
            var txt = this.constructor;
            txt += this.hello + " " + this.world + " " + this.status;
            document.getElementsByTagName('body')[0].innerHTML = txt;
        }
        hello();
    </script>
</body>
</html>

This test attempted to show what type of constructor the function ‘hello’ is on browser, and also the properties ‘hello’, ‘world’ and ‘status’. As the screen shot shown, different browser treats the function object differently, especially Firefox. Latest Firefox (in this case, version 4 beta 6) treated it as an Object. but previous version of Firefox (3.6.3 or older) treated it as an object Window and note that ‘status’ is missing from Firefox’s output as compare to other browsers. This mean Firefox prohibited the usage of property name of “status”. A property name that called “status” can only be used when it is in a Javascript class.

result in various browsers

On the other hand, one foolproof practice is to convert a function object into a new Javascript class like below:

function myFunc() {
    if (!(this instanceof Object)) {
        return new myFunc();
    }
 
    this.hello = "hello";
    this.world = "world";
    this.status = "status";
    return this;
}


This code will fail since Firefox 3.6.8+ updated its instance to Object (as shown in screen shot). As a result, developer need to remove this interlocking code as it’s no longer useful, but the workaround is to call it as a new function class when using it.