전체 페이지뷰

2013년 12월 12일 목요일

javascript function

from. design patterns

In JavaScript the knowledge and proper use of functions is critical. This chapter dis- cussed the background and terminology related to functions. You learned about the two important features of functions in JavaScript, namely:

  1. Functions are first-class objects; they can be passed around as values and augmen- ted with properties and methods.

  2. Functions provide local scope, which other curly braces do not. Also something to keep in mind is that declarations of local variables get hoisted to the top of the local scope.

The syntax for creating functions includes:

  1. Named function expressions

  2. Function expressions (the same as the above, but missing a name), also known as anonymous functions

  3. Function declarations, similar to the function syntax in other languages

After covering the background and syntax of functions, you learned about a number

of useful patterns, which can be grouped into the following categories:

1. API patterns, which help you provide better and cleaner interfaces to your func- tions. 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

2. Initialization patterns, which help you perform initialization and setup tasks (very common when it comes to web pages and applications) in a clearer, structured way without polluting the global namespace with temporary variables. These include:

Immediate functions

Executed 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

3. Performance patterns, which 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 invo- cation and after 

댓글 없음:

댓글 쓰기