(function() {
It is an expression with pronounced immediate function ( IIFE ), pronounced as "iffy". Some people also call them “anonymous, self-fulfilling functions” or simply “self-fulfilling functions”.
(function(aParameter) { alert(aParameter);
Here is IIFE, which takes the parameter "aParameter" and passes the argument "argument".
(function($){ alert($(window).jquery);
This is similar, but "jQuery" (an instance of the jQuery object) is an argument for IIFE, in which case jQuery is passed as the "$" parameter. Thus, simply by typing '$', the IIFE body has access to jQuery and its members. This is a common jQuery convention, and it is common for people who write jQuery plugins to maintain this convention in this way.
This code not only supports the jQuery convention, but also ensures that neither you nor anyone else can accidentally violate this convention. For example, enter the following code:
var $ = function() { alert('foo'); }
This code turns '$' into something that is definitely not jQuery. If someone did this in other code outside your plugin code, and then you obviously did not pass jQuery as "$" to your plugin, then you cannot use "$" as jQuery, as usual.
Hawkeye Parker Oct. 16 2018-11-11T00: 00Z
source share