日期:2014-05-16 浏览次数:20485 次
三.Function
??????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.
???
// 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; };?