技术码头

ES6 函数的扩展

2020-01-07  本文已影响0人  石菖蒲_xl

一、 函数参数的默认值

1、基本用法

ES6允许为函数的参数设置默认值,直接写在参数定义的后面。

function log (t = "hello",value = "world") {
  console.log(t,value)
}
log(); // => "hello world"
log('h'); // => "h world"
log("h","w"); // => "h w"
function foo(x = 5) {
  let x = 1; // error
  const x = 2; // error
}
function a (x,x,y =1){
  // do something
}
// 报错
let x  = 99;
function a (y = x +1) {
  console.log(y);
}
a(); // => 100;
x = 100;
a(); // => 101

2、与结构赋值默认值结合使用

function a({x,y=5} = {}){
  console.log(x,y);
}
a(); // => undefined 5

如下:两个函数都对参数设定了默认值,区别在

// 写法一
function m1({x = 0, y = 0} = {}) {
  return [x, y];
}

// 写法二
function m2({x, y} = { x: 0, y: 0 }) {
  return [x, y];
}
// 函数没有参数的情况
m1() // [0, 0]
m2() // [0, 0]

// x 和 y 都有值的情况
m1({x: 3, y: 8}) // [3, 8]
m2({x: 3, y: 8}) // [3, 8]

// x 有值,y 无值的情况
m1({x: 3}) // [3, 0]
m2({x: 3}) // [3, undefined]

// x 和 y 都无值的情况
m1({}) // [0, 0];
m2({}) // [undefined, undefined]

m1({z: 3}) // [0, 0]
m2({z: 3}) // [undefined, undefined]

2、参数默认值的位置

3、函数的length属性

(function (a) {}).length // 1
(function (a = 5) {}).length // 0
(function (a, b, c = 5) {}).length // 2

4、作用域

var x = 1;
function f(x, y = x) {
  console.log(y);
}
f(2); // => 2

上面代码中,参数y的默认值等于变量x,调用函数f时,参数形成一个单独的作用域,在这个作用域里,默认值变量x指向第一个参数x,而不是全局变量x,所以输出2

5、应用

function throwIfMissing() {
  throw new Error('Missing parameter');
}
function foo(mustBeProvided = throwIfMissing()) {
  return mustBeProvided;
}
foo()
// Error: Missing parameter

二、rest参数

ES6引入rest参数(形式为...变量名),用来获取函数的多余参数,这样就不需要使用arguments对象了

function add(...values){
  console.log(values); // => [2,3,5]
}
add(2,3,5);
// 报错
function f(a, ...b, c) {
  // ...
}
(function(...a) {}).length  // 0
(function(a, ...b) {}).length  // 1

三、箭头函数

var f = v => v;

// 等同于
var f = function (v) {
  return v;
};
var f = () => 5;
var sum = (num1, num2) => num1 + num2;
var sum = (num1, num2) => { return num1 + num2; }
// 报错
let getTempItem = id => { id: id, name: "Temp" };

// 不报错
let getTempItem = id => ({ id: id, name: "Temp" });
const full = ({ first, last }) => first + ' ' + last;

// 等同于
function full(person) {
  return person.first + ' ' + person.last;
}
// 正常函数写法
[1,2,3].map(function (x) {
  return x * x;
});
// 箭头函数写法
[1,2,3].map(x => x * x);

箭头函数使用注意点:
1、函数体内的this对象,就是定义时所在的对象,而不是使用时所在对象
2、不可当构造函数,也就是说,不可以使用new命令
3、不可以使用arguments对象,该对象在函数体内部存在,用rest参数代替
4、不可使用yield命令,因此箭头函数不能做Generator函数


四、尾调用

尾调用是函数式编程的一个重要概念,指某个函数的最后一步是调用另一个函数

function f(x){
  return g(x);
}

五、柯里化

函数式编程有一个概念,叫做柯里化,意思是将多参数的函数转换成单参数的形式

function currying(fn,n){
    return function(m){
        return fn.call(this,m,n); //  调用传入函数本身,并传入值 n 相当于默认值
    }
}
function tail(m,n){
    console.log("n",n);
    console.log("m",m);
}
var f = currying(tail,1); // 传入函数,指定默认值
f(5);
// => "n" 1
// => "m" 5
function tail(m,n = 1){
    console.log("n",n);
    console.log("m",m);
}
tail(5); 
// => "n" 1
// => "m" 5
上一篇 下一篇

猜你喜欢

热点阅读