Js之手写bind

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>

  <script>
    // 手写bind
    Function.prototype.bind1 = function (){
      // 将参数转化未数组
      const args = Array.prototype.slice.call(arguments);

      const t = args.shift();

      const self = this;

      return function (){
        self.apply(t, args);
      }
    }


    function fn1(a, b, c){
      console.log(a, b, c);
      console.log("this", this);
    };

    let fn2 = fn1.bind1({name:'Tom'}, 10, 20, 30)
    fn2()

  </script>
</body>
</html>
Last modification:October 28th, 2020 at 10:13 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment