Function-Related Strict Warnings
Time to explore function-related strict warnings...
Error #1:
- JSMSG_NO_RETURN_VALUE
"function functionName does not always return a value"
As of Netscape 6, a function can either have no return statement or its last statement must be return. That is, a function which returns a value must have that return as the last line, but if it does not return a value at all, it's ok and will not have any warnings. This makes sense, as you never know when a user may state:
var k = myFunc();
Solution: It's generally advisable to have a
response
variable for any non-constructor function.What you return is up to you,
but there are a few good approaches:
- If the function already has a designated return value, make that the last line of your function. (I've discovered situations where a function will unconditionally return something before the last line of the function; JavaScript will still throw a strict warning in those situations.)
- If the function removes something, return the removed value. (This
includes
null
where appropriate.) - If the function is a constructor, append values to
this
, make sure you returnthis
. - If the function has some other purpose and is successful, return
true
. - If the function is not successful, return
false
.
Error #2:
- JSMSG_ANON_NO_RETURN_VALUE
"anonymous function does not always return a value"
This is the same as the "function functionName does not always return a value", except you did not declare a function name in the function definition.
Error #3:
- JSMSG_DUPLICATE_FORMAL
"duplicate formal argument argName"
function f (abc,abc){}
Solution: Simply don't name your function's arguments identically.
Error #4:
- JSMSG_VAR_HIDES_ARG
"variable varName hides argument"
function h(k) { var k = 3 return k }This happens because you used
var
to redeclare the argument
name.
Error #5:
- JSMSG_DEPRECATED_USAGE
"deprecated ___ usage"
This means your usage of the function was not necessarily correct. The browser still supports them, but you cannot expect this in the future. You shouldn't expect to see this too often.
- Tutorial introduction (variable declaration warnings)
- Function related strict warnings
- Miscellaneous strict warnings