- Chapter 1: Introduction
- Patterns
- JavaScript concepts
- ECMAScript 5
- JSLint
- The Console
- Chapter 2: Essentials
- Writing Maintainable Code
- Minimizing Globals
- for Loops
- for-in Loops
- (Not) Augmenting Built-in Prototypes
- switch Pattern
- Avoiding Implied Typecasting
- Avoiding eval()
- Number Conversions with parseInt()
- Coding Conventions
- Naming Conventions
- Writing Comments
- Writing API Docs
- Writing to Be Read
- Peer Reviews
- Minify...In Production
- Run JSLint
- Summary
- Chapter 3: Literals and Constructors
- Object Literal
- Custom Constructor Funtions
- Patterns for Enforcing new
- Array Literal
- JSON
- Regular Expression Literal
- Primitive Wrappers
- Error Objects
- Summary
- Chapter 4: Functions
- Background
- Callback pattern
- Returning functions
- Self-Defining functions
- Immediate functions
- Immediate Object initialization
- Init-Time Branching
- Function Properties - A Memoization Pattern
- Configuration Objects
- Curry
- Summary
- Chapter 5: Object Creation Patterns
- Namespace Pattern
- Declaring Dependencies
- Private Properties and Methods
- Module Pattern
- Sandbox Pattern
- Static Members
- Object Constants
- Chaining Pattern
-
method()
Method - Summary
-
-
Save muhammadghazali/c7ab0ff528b1c48ee3de to your computer and use it in GitHub Desktop.
Chapter 4: Immediate Functions
The immediate function pattern is a syntax that enables you to execute a function as it is defined.
Keep in mind, that the term immediate function is not defined in the ECMAScript standard.
Here is the step-by-steps to create immediate function:
define a function using a function expression
function() {
// TODO write enough code
}
add a set of parentheses at the end
function() {
// TODO write enough code
}()
wrap the whole function in parantheses
(function() {
// TODO write enough code
}());
The following alternative syntax is also common (note the placement of the closing parentheses):
(function () {
alert('watch out!');
})();
Parameters of an immediate function
How to pass an arguments to immediate functions:
(function(parameter1, parameter2){
// TODO write enough code
}("Argument of parameter 1", "Argument of parameter 2"));
Note that in general you shouldn't pass too many parameters to an immediate function, because it could quickly become a burden to constantly scroll to the top and top the bottom of the function to understand how it works.
Returned values from immediate functions
An immediate function can return values and these return values can be assigned to variables:
var result = (function() {
return 2 + 2;
}());
Benefits and usage
- it helps you wrap an amount of work you want to do without leaving any global variables behind.
- enables you to wrap individual features into self-contained modules.
Others
- Other names for the immediate function pattern include self-invoking or self-executing function, because the function executes itself as soon as it's defined.
Chapter 4: Configuration objects
Use configuration object pattern to provide cleaner APIs.
An example of function without configuration objects pattern:
function addPerson(first, last) { ... }
An example of function with configuration objects pattern:
// declaration
function addPerson(conf) { ... }
// usage
addPerson({
username: 'ghanoz',
age: 26
});
The pros of the configuration objects pattern are:
- No need to remember the parameters and their order
- You can safely skip optional parameters
- Easier to read and maintain
- Easier to add and remove parameters
The cons of the configuration objects are:
- You need to remember the names of the parameters
- Property names cannot always be safely minified, especially by simpler minifiers
Chapter 4: Summary
Useful patterns in JavaScript function:
- API patterns
- Initialization patterns
- Performance patterns
API patterns
These pattern help you provide better and cleaner interfaces to your functions. These patterns include:
- Callback patterns. Pass a function as an argument
- Configuration objects. Help keep the number of arguments to a function under control
- Returning functions. When the return value of one function is another function
- Currying. When new functions are created based on existing ones plus a partial list of arguments
Initialization patterns
These pattern help you perform initialization and setup tasks (very common to web pages and applications) in a clearer, structured way without polluting the global namespace with temporary variables. These patterns include:
- Immediate functions. Execute as soon as they are defined
- Immediate object initialization. Initialization tasks structured in an anonymous object that provides a method to be called immediately.
- Init-time branching. Helps branch code only once during initial code execution, as opposed to many times later during the life of the application.
Performance patterns
These patterns help speed up the code. These include:
- Memoization. Using function properties so that computed values are not computed again.
- Self-defining functions. Overwrite themselves with new bodies to do less work from the second invocation and after.
The great things about returning functions in JavaScript is we can have a function returning another more specialized function, or it can create another function on-demand, depending on some inputs.