ES6新技术之 函数技术
Summary Functions haven’t undergone a huge change in ECMAScript 6 but rather a series of incremental changes that make them easier to work with. 积累了相当的ES5函数技术的使用经验,ES6也对函数对象技术作了多处的使用上的改进,让其更加的灵活方便。
Default function parameters allow you to easily specify what value to use when a particular argument isn’t passed. Prior to ECMAScript 6, this would require some extra code inside the function to check for the pres- ence of arguments and assign a different value.
参数
第一大改进是函数形式的参数的使用,包括:
形式参数的默认值
第一,支持定义形式参数的默认值,这样ES6函数在「参数值」上提供了灵活性,节省函数定义时的手动(extra code)检查参数值的存在与否;
function makeRequest(url, timeout = 2000, callback = function() {}) {
// the rest of the function
}
// uses default timeout and callback
makeRequest("/foo");
// uses default callback
makeRequest("/foo", 500);
// doesn't use defaults
makeRequest("/foo", 500, function(body) {
doSomething(body);
});
Rest parameters allow you to specify an array into which all remaining parameters should be placed. Using a real array and letting you indicate which parameters to include makes rest parameters a much more flexible solution than arguments .
余参
第二,提供的余参(Rest parameters )新语法,这样新函数在「参数个数」(number of parameters)上提供了灵活性,一种比ES5 arguments更合理更灵活的方式。余参语法(...restPara)自动将调用函数时提供无名(无形式定义)的参数「收集」到一起,组成一个参数数组。
function pick(object, ...keys) {
let result = Object.create(null);
// 循环不用担心边界,不像arguments包含了所有参数,你循环不希望有的参数
for (let i = 0, len = keys.length; i < len; i++) {
result[keys[i]] = object[keys[i]];
}
return result;
}
例如,ES5 arguments 对形式参数是盲的,不知道函数参数具体定义;而ES6余参明确是“余下的参数”,有一部分参数是明确的。
余参有两项限制:第一,每个函数只有一个余参,且必位于尾部;第二,不用于 对象的setter ,因为setter 只能有一个参数。
参数的默认值(默认参数)让函数可以用「比定义更少的参数」使用函数,而余参则相反,让函数可用「比定义更多的参数」使用函数。
实际参数的展开操作符
第三,是在调用处理数组元素的函数,可使用展开操作符(spread operator ),简化任务; 如果你有一个处理某个数组的函数方法——形式参数是该数组的数组项(例如Math.max(25, 50, 75, 100))——时,现在你不必预先手动拆开数组一个一个的传(或改用数组形式,在函数内重新处理),只需要在调用的时候实参操作展开操作符,系统为自动为你拆开数组,再调用。
这里展开操作符有一些使用上假定,假定,第一,一个有多个数组元素形式参数的函数方法,第二,展开操作只用于实参展开,这个与形式余参是有区别,虽然语法上都是三点(...); 展开操作符除了用于函数实参的展开调用,还可用以数组创建(数组字面量上子数组的展开),和对象创建(子属性展开)。 The spread operator is a companion to rest parameters, allowing you to deconstruct an array into separate parameters when calling a function.
Prior to ECMAScript 6, there were only two ways to pass individual parame- ters contained in an array: by manually specifying each parameter or using apply() . With the spread operator, you can easily pass an array to any func- tion without worrying about the this binding of the function.
Whereas rest parameters allow you to specify that multiple independent arguments should be combined into an array, the spread operator allows you to specify an array that should be split and passed in as separate arguments to a func- tion.
let values = [25, 50, 75, 100]
// equivalent to
// console.log(Math.max(25, 50, 75, 100));
console.log(Math.max(...values));
// 100
新形式——箭头函数
The biggest change to functions in ECMAScript 6 was the addition of arrow functions. Arrow functions are designed to be used in place of anony- mous function expressions. Arrow functions have a more concise syntax, lexical this binding, and no arguments object. Additionally, arrow functions can’t change their this binding and therefore can’t be used as constructors.
ECMAScript 6 中函数的最大变化是增加了箭头函数,一种新函数形式/形态。箭头函数旨在代替匿名函数表达式。箭头函数具有更简洁的语法、自动的词法"this"绑定,并且没有"arguments"对象。此外,箭头函数不能更改其"this"绑定,因此不能用作构造函数。
元属性
The addition of the name property should help you more easily identify functions for debugging and evaluation purposes. ECMAScript 6 also for- mally defines the behavior of block-level functions so they are no longer a syntax error in strict mode. In ECMAScript 6, the behavior of a function is defined by [[Call]] , normal function execution, and [[Construct]] when a function is called with new . The new.target metaproperty also allows you to determine if a function was called using new or not.
尾调优化
Tail call optimization allows some function calls to be optimized to maintain a smaller call stack, use less memory, and prevent stack overflow errors. This optimization is applied by the engine automatically when it is safe to do so; however, you might decide to rewrite recursive functions to take advantage of this optimization.