Created
March 25, 2014 11:25
Using Named Functions Within Self-Executing Function Blocks In Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
( function(){ .... function body .... } )(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
( function myFunction(){ .... function body .... } )(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Named Self-Executing Functions</title> | |
<script type="text/javascript"> | |
// Define a variable to be accessed within the | |
// self-executing function block. | |
var counter = 0; | |
// Create a self-executing function block; however, rather | |
// than passing in an anonymous, headless function, let's | |
// pass in a named function. | |
(function useCounter(){ | |
// Increment and log counter. | |
console.log( ++counter ); | |
// Call *this* function again in after a short timeout. | |
// Since it has a name, we don't have to use the | |
// depracated arguments.callee property. | |
setTimeout( useCounter, 1000 ); | |
})(); // Self-execute. | |
</script> | |
</head> | |
<body> | |
<!-- Intentionally left blank. --> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment