简体   繁体   中英

Closures With Es5 and Es6

var add = function add(x, z) {
  if (z == undefined) {
    return function(y) {
      return x + y;
    }
  } else {
    return x + z
  }
}

The above code work well when i write code with arrow function not work

var add = add(x, z) => {
  if (z == undefined) {
    return (y) => {
      return x + y;
    }
  } else {
    return x + z
  }
}

Just a wrong syntax, as an arrow function should be declared like here

Replace add(x, z) => with (x, z) =>

Please remove the add key word, try to use const and let declaratio because them are block scoping, with var you allocate innecesary memori with the hositing of varables in JS

const add = (x, z) => {
  if (z == undefined) {
    return (y) => {
      return x + y;
    }
  } else {
    return x + z
  }
}

Nothing but wrong syntax. Have fun.

var add = (x, z) => {
  if (z == undefined) {
    return (y) => {
      return x + y;
    }
  } else {
    return x + z;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM