bind函数的实现

Function.prototype.bind = function(){
	let self = this,	//保存当前函数
	    context = [].shift.call(arguments),	//保存当前上下文
	    args = [].slice.call(arguments);	// 保存参数
	
	return function(){
		self.apply(context, [].concat.call(args, [].slice.call(arguments)))
	}
}


    let obj = {
      name: 'xx'
    }
    var fun = function(a,b,c, d){
      console.log(this.name, 'name');
      console.log(a,b,c,d);
    }.bind(obj, 3,4);

    fun(5,6)

bind函数的实现_第1张图片

参考资料:

DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <link rel="icon" href="./vue.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>xx管理系统title>
head>
<body>
  <div id="app">div>
  

  <script>


    Function.prototype.bind = function(){
      let self = this,
          context = [].shift.call(arguments),
          args = [].slice.call(arguments)

          return function(){

            console.log([].concat.apply(arg, [].slice.call(arguments)),   1111111);
            console.log([].concat.call(arg, [].slice.call(arguments)),   2222);
            self.apply(context, [].concat.call(args, [].slice.call(arguments)))
          }
          
      console.log(11111111);
    }

    let obj = {
      name: 'xx'
    }
    var fun = function(a,b,c, d){
      console.log(this.name, 'name');
      console.log(a,b,c,d);
    }.bind(obj, 3,4);

    fun(5,6)



    function ab(a, b, c) {
      console.log(a,b,c);
      console.log(arguments, 99999);

      let xx = [].shift.call(arguments)
      console.log(xx, 'xx');
    }


    ab(1,2,3);

    console.log(ab.length, 66);

  script>
body>
html>

你可能感兴趣的:(javascript,前端,开发语言)