ES6-Extended Summary of Functions-02

1. The name attribute of the function, get the function name

(function aaaa() {
    
    }).name // 'aaaa'

//匿名函数返回空字符串
(function () {
    
    }).name // ''
(function eee() {
    
    }).name // 'eee'

// 构造函数都返回 anonymous
(new Function).name // 'anonymous'

// bind 返回的函数,会在 name之前加上 bound
(function aaa() {
    
    }).bind({
    
    }).name // 'bound aaa'
function www() {
    
    }
www.bind(function eeee() {
    
    }).name // bound www

2. Arrow function

① 箭头函数 没有自己的 this, 里面的 this 是固定的,指向运行时所在的作用域

② 箭头函数不能作为构造函数,不能用 new 命令,否则报错

③ 箭头函数没有 yield 命令,不能用作持 Generate 函数

④ 箭头函数不能使用 arguments,可以用 rest 来代替

3, tail call function: refers to the last step is to call the function

// 尾调函数
function aaa1() {
    
    
  return bbb() // 最后一步调用函数,称为尾调用
}
// 尾调函数
function aaa2(n) {
    
    
  if(n > 111){
    
    
    return bbb() // 最后一步调用函数,称为尾调用(不一定是函数的最后一步)
  }
  return bbb1() // 最后一步调用函数,称为尾调用(不一定是函数的最后一步)
}
function bbb() {
    
    }
function bbb1() {
    
    }


// 非尾调函数
function a1() {
    
    
  return bbb() + 1 //最后一步是加法操作
}
// 非尾调函数
function a2() {
    
    
  let y = bbb()
  return y //最后一步是赋值操作
}
// 非尾调函数
function a3() {
    
    
  bbb() //这个相当于是执行这些代码:  bbb(); return undefined;
}

4. Tail call optimization: When you do not need to use the variables of the outer function, directly replace the outer function with the inner function. This process is tail call optimization.
Only tail-calling functions can be implemented. Only supported by safari, not supported by chrome and firefox

function a(){
    
    
    return b(3)
}
function b(x) {
    
    
  console.log(x)
}
a()

// 对函数 a 进行 尾调用优化,直接用内层函数 b 取代外层函数 a
function b(x) {
    
    
  console.log(x)
}
b(3)

5. Tail recursion: A tail-calling function calls itself

function x(n) {
    
    
  return n * x(n - 1)
}

Just record this, I wish you all a happy~
insert image description here

Guess you like

Origin blog.csdn.net/qq_37600506/article/details/123271730