one stevy boi
one stevy boi

Reputation: 576

JavaScript scope discrepancy between Chrome and Firefox

I wrote a jQuery plugin to force numerical values in input fields, and it works fine in Chrome and Safari but not in Firefox. I have posted a piece of the script below, and in Firefox it fails on the first line, with the error ReferenceError: setDefault is not defined. Why wouldn't this line work in Firefox?

Link to full script

isDollar = setDefault(options.isDollar, isDollar);
isReal = setDefault(options.isReal, isReal);
allowDecimal = setDefault(options.allowDecimal, allowDecimal);
allowNegative = setDefault(options.allowNegative, allowNegative);
maxDigits = setDefault(options.maxDigits, maxDigits);

function setDefault(myVar, defaultVal){
    if(typeof myVar === "undefined") myVar = defaultVal;
    return myVar;
}

Upvotes: 1

Views: 80

Answers (1)

one stevy boi
one stevy boi

Reputation: 576

Seems Firefox has a much more strict interpretation of ECMA standards. What I left out of the code snippet is that the whole thing is inside of an if block. Apparently, in that case, JavaScript executed in Firefox will not be able to call setDefault before it is declared. Interesting that this code will work in other browsers but explode in Firefox. Lesson learned.

Upvotes: 1

Related Questions