javascript - What is the purpose of passing-in undefined? -
i have noticed jquery
, related keynote plugins jquery.ui
pass undefined
parameter anonymous functions used in module definitions, so:
(function($, undefined) { ... })(jquery);
alternatively, have noticed other plugins recommended jquery and/or others not pass undefined in parameter.
this silly question, but...
shouldn't available anyway? why pass in? there sort of other purpose or trick going on here?
there 2 reasons that:
1) if undefined
variable in function scope rather global object property, minifiers can reduce single letter achieving better compression rate.
2) before es5*, undefined
property of global object without write-protection. overwritten, resulting in strange , unexpected behavior. this:
var test = 123; undefined = 123; if (test === undefined){ // true, since undefined equals 123 }
by having function argument undefined
(the name not matter) don't pass parameter to, make sure have variable undefined
can test "undefinedness" against variable.
btw. safest method test undefined is: typeof ( var ) === 'undefined'
(*) ecmascript 5, global properties undefined
, nan
, infinity
became readonly. general adoption in modern browsers - of course exception of ie 9 - overwriting values not possible anymore.
Comments
Post a Comment