读书人

JavaScript Patterns 读书笔记(3)

发布时间: 2012-10-06 17:34:01 作者: rapoo

JavaScript Patterns 读书笔记(三)

三.Function

Background
???? There are two main features of the functions in JavaScript that make them special—the first is that functions are first-class objects and the second is that they provide scope.Functions are objects that:

Can be created dynamically at runtime, during the execution of the program.Can be assigned to variables, can have their references copied to other variables,?can be augmented, and, except for a few special cases, can be deleted.Can be passed as arguments to other functions and can also be returned by other functions.Can have their own properties and methods.

??????In general, when you think of a function in JavaScript, think of an object, with the only special feature that this object is invokable, meaning it can be executed.The fact that functions are objects becomes obvious when you see the new Function() constructor in action:

?

????The name property is useful when debugging code in Firebug or other debuggers. When the debugger needs to show you an error in a function, it can check for the presence of the name property and use it as an indicator. The name property is also used to call the same function recursively from within itself. If you were not interested in these two cases, then an unnamed function expression would be easier and less verbose.? The case against function declarations and the reason to prefer function expressions is that the expressions highlight that functions are objects like all other objects and not some special language construct.
??? ?It’s technically possible to use a named function expression and assign?it to a variable with a different name, for example:

??? Commonly, the global object is passed as an argument to the immediate function so that it’s accessible inside of the function without having to use window: this way makes the code more interoperable in environments outside the browser:
???? In this example, o.message is a string property, not a function, but it needs a?function,which executes while the script is loading and which helps define the property.
Immediate Object Initialization
?? Another way to protect from global scope pollution, similar to the immediate functions pattern previously described, is the following immediate object initialization pattern.This pattern uses an object with an init() method, which is executed immediately after the object is created. The init() function takes care of all initialization tasks.Here’s an example of the immediate object pattern:
? ??In terms of syntax, you approach this pattern as if you’re creating a normal object using the object literal. You also wrap the literal in parentheses (grouping operator), which?instructs the JavaScript engine to treat the curly braces as object literal, not as a code?block. (It’s not an if or a for loop.) After you close the parentheses, you invoke the?init() method immediately.
??You can also wrap the object and the init() invocation into grouping parentheses?instead of wrapping the object only. In other words, both of these work:

? You need to remember the names of the parameters? Property names cannot be minified
? ?This pattern could be useful when your function creates DOM elements, for example,or in setting the CSS styles of an element, because elements and styles can have a great number of mostly optional attributes and properties.

读书人网 >JavaScript

热点推荐