日期:2014-05-16  浏览次数:20485 次

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:

?

// antipattern
// for demo purposes only
var add = new Function('a, b', 'return a + b');
add(1, 2); // returns 3

???????
??????In this code, there’s no doubt that add() is an object; after all it was created by a constructor. Using the Function() constructor is not a good idea though (it’s as bad as eval()) because code is passed around as a string and evaluated. It’s also inconvenient to write (and read) because you have to escape quotes and take extra care if you want to properly indent the code inside the function for readability. The second important feature is that functions provide scope. In JavaScript there’s no curly braces local scope(块级作用域,curly brace=大括号); in other words, blocks don’t create scope. There’s only function?scope. Any variable defined with var inside of a function is a local variable, invisible outside the function. Saying that curly braces don’t provide local scope means that if you define a variable with var inside of an if condition or inside of a for or a while loop, that doesn’t mean the variable is local to that if or for. It’s only local to the wrapping function, and if there’s no wrapping function, it becomes a global variable.

???

  • Disambiguation of Terminology (消除术语上的歧义)
    ?? Let’s take a moment to discuss the terminology surrounding the code used to define a function, because using accurate and agreed-upon names is just as important as the code when talking about patterns. Consider the following snippet:
    // named function expression
    var add = function add(a, b) {
    	return a + b;
    };
    
    ? ?The preceding code shows a function, which uses a named function expression. If you skip the name (the second add in the example) in the function expression, you get an unnamed function expression, also known as simply as function expression or most commonly as an anonymous function. An example is:
    ?
    // function expression, a.k.a. anonymous function
    var add = function (a, b) {
    	return a + b;
    };
    
    ?
    ?? So the broader term is “function expression” and the “named function expression” is a specific case of a function expression, which happens to define the optional name. When you omit the second add and end up with an unnamed function expression, this won’t affect the definition and the consecutive invocations of the function. The only difference is that the name property of the function object will be a blank string. The name property is an extension of the language (it’s not part of the ECMA standard) but widely available in many environments. If you keep the second add, then the property add.name will contain the string “add.” The name property is useful when using debuggers, such as Firebug, or when calling the same function recursively from itself; otherwise you can just