I want expand my last post (Immediately invoked function expression aka IIFE), by more examples of using IIFE.

IIFE can be also used like this:

var foo = function() {
// code
}();

However, it’s better practice to do this like this, because of code readability:

var foo = (function() {
// long code
})();

If you have on your page jQuery and maybe other JS library that uses $ sing to access it. You may use this little pattern to pass library to IIFE, the way it will not make conflicts. And also get advantage of $ access to that library:

(function($) {
// code using $
})(jQuery);

Also it helps with rendering animations:

(function animation_loop() {
    window.requestAnimationFrame(animation_loop)
    render():
})();

It’s protect against unwanted globals:

(function() {
     window.public = 'foo';
     var private = 'bar';
})();

// latter on

public; // foo
private; // undefined

Useful if you want compute and return value in one statement:

var browser = {
    vendor_prefix : (function() {
        var prefix;
        // compute stuff
        return prefix;
    })()
};

Create a class in one statement:

var MyClass = (function() {
    function MyClass(foo) {
        this.foo = foo;
    }

    MyClass.prototype = {
        bar : 'baz'
    };

    return MyClass;
})();

Creating private variables:

var number = (function() {

    var num =0,
    add = function(n) { num = num + n;},
    get = function() { return num; };

    return { add:add, get:get};
})();

create private functions:

var foo = (function() {
    var priv = function() { alert('Hi'); },
        publ = function() { priv(); }

    return publ;
})();

Sources:

#!/bin/bash

DETECTOR=`/bin/arcconf getconfig 1 |grep -i optimal |wc -l`

if [ "$DETECTOR" -ne "2" ] 
then

	echo  "`/bin/arcconf getconfig 1`" | mail -s "There is problem with RAID" [email protected]

fi

Of course, you must add it to cron, for example like this:

*/10 * * * * /usr/local/bin/raid_check.sh

This is what: arcconf getconfig 1  command returns:

Bellow command, show information about RAID configuration and status, and also about logical and physical devices.

server:~# arcconf getconfig 1
Controllers found: 1
----------------------------------------------------------------------
Controller information
----------------------------------------------------------------------
   Controller Status                        : Optimal
   Channel description                      : SAS/SATA
   Controller Model                         : Adaptec 5405
   Controller Serial Number                 : 9D31112A78D
   Physical Slot                            : 49
   Temperature                              : 75 C/ 167 F (Normal)
   Installed memory                         : 256 MB
   Copyback                                 : Disabled
   Background consistency check             : Disabled
   Automatic Failover                       : Enabled
   Global task priority                     : High
   Performance Mode                         : Default/Dynamic
   Stayawake period                         : Disabled
   Spinup limit internal drives             : 0
   Spinup limit external drives             : 0
   Defunct disk drive count                 : 0
   Logical devices/Failed/Degraded          : 1/0/0
   SSDs assigned to MaxIQ Cache pool        : 0
   Maximum SSDs allowed in MaxIQ Cache pool : 8
   NCQ status                               : Enabled
   --------------------------------------------------------
   Controller Version Information
   --------------------------------------------------------
   BIOS                                     : 5.2-0 (17899)
   Firmware                                 : 5.2-0 (17899)
   Driver                                   : 1.1-5 (2461)
   Boot Flash                               : 5.2-0 (17899)
   --------------------------------------------------------
   Controller Battery Information
   --------------------------------------------------------
   Status                                   : Not Installed

----------------------------------------------------------------------
Logical device information
----------------------------------------------------------------------
Logical device number 0
   Logical device name                      : Device 0
   RAID level                               : 5
   Status of logical device                 : Optimal
   Size                                     : 342890 MB
   Stripe-unit size                         : 256 KB
   Read-cache mode                          : Enabled
   MaxIQ preferred cache setting            : Enabled
   MaxIQ cache setting                      : Disabled
   Write-cache mode                         : Enabled (write-back)
   Write-cache setting                      : Enabled (write-back)
   Partitioned                              : Yes
   Protected by Hot-Spare                   : No
   Bootable                                 : Yes
   Failed stripes                           : No
   Power settings                           : Disabled
   --------------------------------------------------------
   Logical device segment information
   --------------------------------------------------------
   Segment 0                                : Present (0,0) 10486536320009980002
   Segment 1                                : Present (0,1) 11096504340004610020
   Segment 2                                : Present (0,2) 1110652233000461006C
   Segment 3                                : Present (0,3) 10486536320009980024

----------------------------------------------------------------------
Physical Device information
----------------------------------------------------------------------
      Device #0
         Device is a Hard drive
         State                              : Online
         Supported                          : Yes
         Transfer Speed                     : SATA 3.0 Gb/s
         Reported Channel,Device(T:L)       : 0,0(0:0)
         Reported Location                  : Connector 0, Device 0
         Vendor                             :
         Model                              : Corsair CSSD-F12
         Firmware                           : 2.0
         Serial number                      : 10486536320009980002
         Size                               : 114473 MB
         Write Cache                        : Enabled (write-back)
         FRU                                : None
         S.M.A.R.T.                         : No
         S.M.A.R.T. warnings                : 0
         Power State                        : Full rpm
         Supported Power States             : Full rpm,Powered off
         SSD                                : Yes
         MaxIQ Cache Capable                : No
         MaxIQ Cache Assigned               : No
         NCQ status                         : Enabled
      Device #1
         Device is a Hard drive
         State                              : Online
         Supported                          : Yes
         Transfer Speed                     : SATA 3.0 Gb/s
         Reported Channel,Device(T:L)       : 0,1(1:0)
         Reported Location                  : Connector 0, Device 1
         Vendor                             :
         Model                              : Corsair CSSD-F12
         Firmware                           : 2.0
         Serial number                      : 11096504340004610020
         Size                               : 114473 MB
         Write Cache                        : Enabled (write-back)
         FRU                                : None
         S.M.A.R.T.                         : No
         S.M.A.R.T. warnings                : 0
         Power State                        : Full rpm
         Supported Power States             : Full rpm,Powered off
         SSD                                : Yes
         MaxIQ Cache Capable                : No
         MaxIQ Cache Assigned               : No
         NCQ status                         : Enabled
      Device #2
         Device is a Hard drive
         State                              : Online
         Supported                          : Yes
         Transfer Speed                     : SATA 3.0 Gb/s
         Reported Channel,Device(T:L)       : 0,2(2:0)
         Reported Location                  : Connector 0, Device 2
         Vendor                             :
         Model                              : Corsair CSSD-F12
         Firmware                           : 2.0
         Serial number                      : 1110652233000461006C
         Size                               : 114473 MB
         Write Cache                        : Enabled (write-back)
         FRU                                : None
         S.M.A.R.T.                         : No
         S.M.A.R.T. warnings                : 0
         Power State                        : Full rpm
         Supported Power States             : Full rpm,Powered off
         SSD                                : Yes
         MaxIQ Cache Capable                : No
         MaxIQ Cache Assigned               : No
         NCQ status                         : Enabled
      Device #3
         Device is a Hard drive
         State                              : Online
         Supported                          : Yes
         Transfer Speed                     : SATA 3.0 Gb/s
         Reported Channel,Device(T:L)       : 0,3(3:0)
         Reported Location                  : Connector 0, Device 3
         Vendor                             :
         Model                              : Corsair CSSD-F12
         Firmware                           : 2.0
         Serial number                      : 10486536320009980024
         Size                               : 114473 MB
         Write Cache                        : Enabled (write-back)
         FRU                                : None
         S.M.A.R.T.                         : No
         S.M.A.R.T. warnings                : 0
         Power State                        : Full rpm
         Supported Power States             : Full rpm,Powered off
         SSD                                : Yes
         MaxIQ Cache Capable                : No
         MaxIQ Cache Assigned               : No
         NCQ status                         : Enabled

Command completed successfully.

Shorter version of this command, exactly: arcconf getconfig 1 ld  , showing only logical device information. This means it only show information about RAID:

server:~# arcconf getconfig 1 ld
Controllers found: 1
----------------------------------------------------------------------
Logical device information
----------------------------------------------------------------------
Logical device number 0
   Logical device name                      : Device 0
   RAID level                               : 5
   Status of logical device                 : Optimal
   Size                                     : 342890 MB
   Stripe-unit size                         : 256 KB
   Read-cache mode                          : Enabled
   MaxIQ preferred cache setting            : Enabled
   MaxIQ cache setting                      : Disabled
   Write-cache mode                         : Enabled (write-back)
   Write-cache setting                      : Enabled (write-back)
   Partitioned                              : Yes
   Protected by Hot-Spare                   : No
   Bootable                                 : Yes
   Failed stripes                           : No
   Power settings                           : Disabled
   --------------------------------------------------------
   Logical device segment information
   --------------------------------------------------------
   Segment 0                                : Present (0,0) 10486536320009980002
   Segment 1                                : Present (0,1) 11096504340004610020
   Segment 2                                : Present (0,2) 1110652233000461006C
   Segment 3                                : Present (0,3) 10486536320009980024

Command completed successfully.

Last command: arcconf getconfig 1 AL , show information about all kinds of devices, this means, controller itself, RAID status and configuration, and also logical and physical devices:

server:~# arcconf getconfig 1 AL
Controllers found: 1
----------------------------------------------------------------------
Controller information
----------------------------------------------------------------------
   Controller Status                        : Optimal
   Channel description                      : SAS/SATA
   Controller Model                         : Adaptec 5405
   Controller Serial Number                 : 9D31112A78D
   Physical Slot                            : 49
   Temperature                              : 75 C/ 167 F (Normal)
   Installed memory                         : 256 MB
   Copyback                                 : Disabled
   Background consistency check             : Disabled
   Automatic Failover                       : Enabled
   Global task priority                     : High
   Performance Mode                         : Default/Dynamic
   Stayawake period                         : Disabled
   Spinup limit internal drives             : 0
   Spinup limit external drives             : 0
   Defunct disk drive count                 : 0
   Logical devices/Failed/Degraded          : 1/0/0
   SSDs assigned to MaxIQ Cache pool        : 0
   Maximum SSDs allowed in MaxIQ Cache pool : 8
   NCQ status                               : Enabled
   --------------------------------------------------------
   Controller Version Information
   --------------------------------------------------------
   BIOS                                     : 5.2-0 (17899)
   Firmware                                 : 5.2-0 (17899)
   Driver                                   : 1.1-5 (2461)
   Boot Flash                               : 5.2-0 (17899)
   --------------------------------------------------------
   Controller Battery Information
   --------------------------------------------------------
   Status                                   : Not Installed

----------------------------------------------------------------------
Logical device information
----------------------------------------------------------------------
Logical device number 0
   Logical device name                      : Device 0
   RAID level                               : 5
   Status of logical device                 : Optimal
   Size                                     : 342890 MB
   Stripe-unit size                         : 256 KB
   Read-cache mode                          : Enabled
   MaxIQ preferred cache setting            : Enabled
   MaxIQ cache setting                      : Disabled
   Write-cache mode                         : Enabled (write-back)
   Write-cache setting                      : Enabled (write-back)
   Partitioned                              : Yes
   Protected by Hot-Spare                   : No
   Bootable                                 : Yes
   Failed stripes                           : No
   Power settings                           : Disabled
   --------------------------------------------------------
   Logical device segment information
   --------------------------------------------------------
   Segment 0                                : Present (0,0) 10486536320009980002
   Segment 1                                : Present (0,1) 11096504340004610020
   Segment 2                                : Present (0,2) 1110652233000461006C
   Segment 3                                : Present (0,3) 10486536320009980024

----------------------------------------------------------------------
Physical Device information
----------------------------------------------------------------------
      Device #0
         Device is a Hard drive
         State                              : Online
         Supported                          : Yes
         Transfer Speed                     : SATA 3.0 Gb/s
         Reported Channel,Device(T:L)       : 0,0(0:0)
         Reported Location                  : Connector 0, Device 0
         Vendor                             :
         Model                              : Corsair CSSD-F12
         Firmware                           : 2.0
         Serial number                      : 10486536320009980002
         Size                               : 114473 MB
         Write Cache                        : Enabled (write-back)
         FRU                                : None
         S.M.A.R.T.                         : No
         S.M.A.R.T. warnings                : 0
         Power State                        : Full rpm
         Supported Power States             : Full rpm,Powered off
         SSD                                : Yes
         MaxIQ Cache Capable                : No
         MaxIQ Cache Assigned               : No
         NCQ status                         : Enabled
      Device #1
         Device is a Hard drive
         State                              : Online
         Supported                          : Yes
         Transfer Speed                     : SATA 3.0 Gb/s
         Reported Channel,Device(T:L)       : 0,1(1:0)
         Reported Location                  : Connector 0, Device 1
         Vendor                             :
         Model                              : Corsair CSSD-F12
         Firmware                           : 2.0
         Serial number                      : 11096504340004610020
         Size                               : 114473 MB
         Write Cache                        : Enabled (write-back)
         FRU                                : None
         S.M.A.R.T.                         : No
         S.M.A.R.T. warnings                : 0
         Power State                        : Full rpm
         Supported Power States             : Full rpm,Powered off
         SSD                                : Yes
         MaxIQ Cache Capable                : No
         MaxIQ Cache Assigned               : No
         NCQ status                         : Enabled
      Device #2
         Device is a Hard drive
         State                              : Online
         Supported                          : Yes
         Transfer Speed                     : SATA 3.0 Gb/s
         Reported Channel,Device(T:L)       : 0,2(2:0)
         Reported Location                  : Connector 0, Device 2
         Vendor                             :
         Model                              : Corsair CSSD-F12
         Firmware                           : 2.0
         Serial number                      : 1110652233000461006C
         Size                               : 114473 MB
         Write Cache                        : Enabled (write-back)
         FRU                                : None
         S.M.A.R.T.                         : No
         S.M.A.R.T. warnings                : 0
         Power State                        : Full rpm
         Supported Power States             : Full rpm,Powered off
         SSD                                : Yes
         MaxIQ Cache Capable                : No
         MaxIQ Cache Assigned               : No
         NCQ status                         : Enabled
      Device #3
         Device is a Hard drive
         State                              : Online
         Supported                          : Yes
         Transfer Speed                     : SATA 3.0 Gb/s
         Reported Channel,Device(T:L)       : 0,3(3:0)
         Reported Location                  : Connector 0, Device 3
         Vendor                             :
         Model                              : Corsair CSSD-F12
         Firmware                           : 2.0
         Serial number                      : 10486536320009980024
         Size                               : 114473 MB
         Write Cache                        : Enabled (write-back)
         FRU                                : None
         S.M.A.R.T.                         : No
         S.M.A.R.T. warnings                : 0
         Power State                        : Full rpm
         Supported Power States             : Full rpm,Powered off
         SSD                                : Yes
         MaxIQ Cache Capable                : No
         MaxIQ Cache Assigned               : No
         NCQ status                         : Enabled

Command completed successfully.

 

This time I want show you something interesting from jQuery source code:

(function( window, undefined ) {

})( window );

It’s called immediately invoked function expression aka IIFE. Called also, but these names are are a little bit incorrect:

  • self executing anonymous function
  • self invoking anonymous function

Advantages of using this pattern are:

  • In ECMAScript 3 undefined variable is mutable. And as a result of it, you can change it’s value which can lead to bad comparison results. When you will need compare for example:  foo === undefined. Using this pattern you guarantee, that undefined variable in scope of this self executing anonymous function got correct value. Independently of earlier changes of this variable.  This was achieved by not passing while executing undefined value to function body by parameters. In this way undefined in scope of function is “really” undefinded. It’s weird, but it works :)
  • little better performance, because of fasten identifier resolution process
  • better compression, because compressor can change window variable to single letter, and thus is able to save some byes
  • protecting against unwanted globals from leaking
  • Allow you compute and return value when only one statement is allowed
  • Require to emulate block scope (pre E6’s ‘let’)
  • useful when creating private functions and variables using closures

Newer and considered better version of this pattern is:

(function(window, document, undefined){

})(window, document)

It’s better than above solution, because has all it advantages, but benefits of compression and performance also apply to document variable.

At end, I want say a bit about, from where this pattern comes from. It comes from what is called IIFE

(function() {

})(); // <-- brackets immediately invoke function

This code simply create new function expression and immediately invoke, run it using () at end.

Same way will work these examples:

!function() {

}();
+function() {

}();
-function() {

}();
~function() {

}();
new function(arg) {

}(arg);
(function() {

}).call();

 

All solution will work the same way in all browsers, we using () insead of !,+ or other. Because this  is simply a convention.

Interesting materials about this topic: