- 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
Last active
August 29, 2015 14:01
-
-
Save muhammadghazali/c7ab0ff528b1c48ee3de to your computer and use it in GitHub Desktop.
My reading progress on JavaScript Patterns (http://shop.oreilly.com/product/9780596806767.do)
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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
add a set of parentheses at the end
wrap the whole function in parantheses
The following alternative syntax is also common (note the placement of the closing parentheses):
Parameters of an immediate function
How to pass an arguments to immediate functions:
Returned values from immediate functions
An immediate function can return values and these return values can be assigned to variables:
Benefits and usage
Others