Function

A return keyword without an expression after it will cause the function to return undefined. Functions that don’t have a return statement at all, similarly return undefined.

Bingdings

Bindings declared with let and const are in fact local to the block that they are declared in, so if you create one of those inside of a loop, the code before and after the loop cannot “see” it.
image.png
在for()内用let声明的变量,其scope在{}内

函数提升

  1. console.log("The future says:", future());
  2. function future() {
  3. return "You'll never have flying cars";
  4. }

Arrow functions

  1. const power = (base, exponent) => {
  2. let result = 1;
  3. for (let count = 0; count < exponent; count++) {
  4. result *= base;
  5. }
  6. return result;
  7. };
  8. // 下面两个等价
  9. const square1 = (x) => { return x * x; };
  10. const square2 = x => x * x;

Optional arguments

  1. function fn(a, b) {do something}
  2. fn(a,b,c,d) // 传多的参数会被忽略
  3. fn(a) // 少传的参数为undefined
  4. function fn(a, b = 2) // 设置默认值
  5. {do something}

The call stack

  1. function chicken() {
  2. return egg();
  3. }
  4. function egg() {
  5. return chicken();
  6. }
  7. console.log(chicken() + " came first.");
  8. // → ??

Closure闭包

  1. function wrapValue(n) {
  2. let local = n;
  3. return () => local;
  4. }
  5. // wrap1和wrap2是wrapValue返回的函数
  6. let wrap1 = wrapValue(1);
  7. let wrap2 = wrapValue(2);
  8. console.log(wrap1());
  9. // → 1
  10. console.log(wrap2());
  11. // → 2
  12. function multiplier(factor) {
  13. return number => number * factor;
  14. }
  15. let twice = multiplier(2);
  16. console.log(twice(5));
  17. // → 10

This feature—being able to reference a specific instance of a local binding in an enclosing scope—is called closure. A function that references bindings from local scopes around it is called a closure.

A good mental model is to think of function values as containing both the code in their body and the environment in which they are created. When called, the function body sees the environment in which it was created, not the environment in which it is called.

Recursion递归

  1. function findSolution(target) {
  2. function find(current, history) {
  3. if (current == target) {
  4. return history;
  5. } else if (current > target) {
  6. return null;
  7. } else {
  8. return find(current + 5, `(${history} + 5)`) ||
  9. find(current * 3, `(${history} * 3)`);
  10. }
  11. }
  12. return find(1, "1");
  13. }
  14. console.log(findSolution(24));
  15. // → (((1 * 3) + 5) * 3)

exercises

1. minimum

  1. let min = (a,b) => a > b ? b : a;
  2. console.log(min(0, 10));
  3. console.log(min(0, -10));

2. isEven

  1. function isEven(x) {
  2. if (x === 0) return true
  3. else if (x === 1) return false
  4. else if (x < 0) return isEven(-x)
  5. else return isEven(x - 2)
  6. }
  7. console.log(isEven(50));
  8. // → true
  9. console.log(isEven(75));
  10. // → false
  11. console.log(isEven(-1));

3. Bean counting

  1. function countBs(str) {
  2. return countChar(str, 'B')
  3. }
  4. function countChar(str, char) {
  5. let count = 0
  6. for (let i = 0; i < str.length; i++)
  7. if (str[i] === char) count += 1
  8. return count
  9. }
  10. console.log(countBs("BBC"));
  11. // → 2
  12. console.log(countChar("kakkerlak", "k"));
  13. // → 4