• 介绍一下call,apply,bind方法实现,源于MDN中的bind


    首先看一下call方法
    const slice = Array.prototype.slice
    Function.prototype.MyCall = function(thisObj){
    console.log(thisObj) // 这里面就是实例化对象
    console.log(this) //这里面就是Cluster
    let obj = {}
    obj = thisObj || window
    const args = slice.call(arguments,1)
    obj.fn = this
    obj.fn(...args)
    delete obj.fn
    }
    function Person(){
    this.name = 'bai'
    Cluster.MyCall(this)
    }
    function Cluster(){
    this.name = 'cluster'
    this.action = function(){
    return '吃饭'
    }
    }
    const person = new Person()
    console.log(person.action())
    //我们想想call,apply的区别无非就是第二参数一个是数组(apply),另一个不是
    只要将obj.fn这改为obj.fn(Array.from(args))
    <script>
    var slice = Array.prototype.slice;
    Function.prototype.bind = function() {
    var thatFunc = this, thatArg = arguments[0];
    var args = slice.call(arguments, 1);
    if (typeof thatFunc !== 'function') {
    // closest thing possible to the ECMAScript 5
    // internal IsCallable function
    throw new TypeError('Function.prototype.bind - ' +'what is trying to be bound is not callable');
    }
    return function(){
    var funcArgs = args.concat(slice.call(arguments))
    return thatFunc.apply(thatArg, funcArgs);
    };
    };
    function output(...s){
    console.log(this,s)
    }
    output.bind(123,'EE','DD')(1,2,3)
    </script>
     
    介绍下栈溢出
    function sum(x, y) {
        if (y > 0) {
            return sum(x + 1, y - 1);
        else {
            return x;
        }
    }
     
    sum(1, 100000000000)
     
    这个时候会出现一个堆栈溢出的错误,在es6里面,有一个为递归优化的方法可以解决,即在最后一步调用函数,且实现函数的柯里化(多参函数转换成单参数函数),但是需要开启严哥模式,普通模式下会报错,这个时候我再阮大神的es6书籍里面,看到了蹦床函数,结合.bind,使函数调用的时候是自己的方法,但是确是另一个函数对象,不是本身,这个时候就不会造成内存的泄露,发生堆栈溢出了,实现代码如下:
     
    function trampoline(f) {
      while (f && f instanceof Function) {
        f = f();
      }
      return f;
    }
     
     
    function sum(x, y) {
      if (y > 0) {
        return sum.bind(null, x + 1, y - 1);
      else {
        return x;
      }
    }
     
    trampoline(sum(1, 100000))
  • 相关阅读:
    SkinSharp用法
    nosql和关系型数据库比较?
    Java实现 蓝桥杯VIP 算法提高 进制转换
    Java实现 蓝桥杯VIP 算法提高 3-2字符串输入输出函数
    Java实现 蓝桥杯VIP 算法提高 3-2字符串输入输出函数
    Java实现 蓝桥杯VIP 算法提高 3-2字符串输入输出函数
    Java实现 蓝桥杯VIP 算法提高 3-2字符串输入输出函数
    Java实现 蓝桥杯VIP 算法提高 3-2字符串输入输出函数
    Java实现 蓝桥杯VIP 算法提高 去注释
    Java实现 蓝桥杯VIP 算法提高 去注释
  • 原文地址:https://www.cnblogs.com/MDGE/p/12038646.html
Copyright © 2020-2023  润新知