javascriptfirefox-5

Weird javascript behaviour FireFox 5.0


I've had a javascript running for quite som time and I've never experienced any problems in IE or in earlier version of firefox. In Firefox 5.0 however, if fails.

Note! The formula has been removed to simplify the example, as I've confirmed that the formula is working and producing the expected result.

<script type="text/javascript">
    function NaN(Num) {
        return (isNaN(Num) || Num == Infinity || Num == -Infinity ? "n/a" : Num);
    }

    function CalculateFormula() {
        result = NaN(*formula*);
    }

</script>

When the NaN function is to be called the javascript just exit. It works like a charm in IE, but to support FireFox 5.0 I've removed the NaN function from the result.

function CalculateFormula() {
    result = *formula*;
}

I've also tried this, but it also will exit from the script when calling the NaN function. But this confirmed that my formula returned a number as expected.

function CalculateFormula() {
    result = *formula*;
    result = NaN(result);
}

Anyone who can shed some light on this issue?


Solution

  • It's failing on Firefox because the NaN property of the global object is defined on ECMAScript 5 as non-writable, non-configurable and non-enumerable, and Firefox 4 (and up) is implementing this version of the specification.

    This means that the value of that property cannot be changed with an assignment -non-writable-, the property cannot be reconfigured (cannot change any of the three mentioned attributes) or being deleted -non-configurable-, and it will not appear on a for-in loop or with the Object.keys method -non-enumerable-.

    You can rename your function or better yet, restructure your code in order to expose less global identifiers to avoid the problem.

    Value properties of the global object as NaN, Infinity and undefined, were writable on ECMAScript 3, and this caused a lot of issues, you couldn't rely on its values, ES5 fixes the problem.