<small id='4Tcr9'></small><noframes id='4Tcr9'>

    <i id='4Tcr9'><tr id='4Tcr9'><dt id='4Tcr9'><q id='4Tcr9'><span id='4Tcr9'><b id='4Tcr9'><form id='4Tcr9'><ins id='4Tcr9'></ins><ul id='4Tcr9'></ul><sub id='4Tcr9'></sub></form><legend id='4Tcr9'></legend><bdo id='4Tcr9'><pre id='4Tcr9'><center id='4Tcr9'></center></pre></bdo></b><th id='4Tcr9'></th></span></q></dt></tr></i><div id='4Tcr9'><tfoot id='4Tcr9'></tfoot><dl id='4Tcr9'><fieldset id='4Tcr9'></fieldset></dl></div>

      <legend id='4Tcr9'><style id='4Tcr9'><dir id='4Tcr9'><q id='4Tcr9'></q></dir></style></legend>
      <tfoot id='4Tcr9'></tfoot>

        • <bdo id='4Tcr9'></bdo><ul id='4Tcr9'></ul>

        JS中call apply bind函数手写实现demo

        下面是关于“JS中call apply bind函数手写实现demo”的攻略:

        <legend id='MCnN8'><style id='MCnN8'><dir id='MCnN8'><q id='MCnN8'></q></dir></style></legend>
            <tfoot id='MCnN8'></tfoot>
          • <small id='MCnN8'></small><noframes id='MCnN8'>

              <bdo id='MCnN8'></bdo><ul id='MCnN8'></ul>
                <tbody id='MCnN8'></tbody>

                <i id='MCnN8'><tr id='MCnN8'><dt id='MCnN8'><q id='MCnN8'><span id='MCnN8'><b id='MCnN8'><form id='MCnN8'><ins id='MCnN8'></ins><ul id='MCnN8'></ul><sub id='MCnN8'></sub></form><legend id='MCnN8'></legend><bdo id='MCnN8'><pre id='MCnN8'><center id='MCnN8'></center></pre></bdo></b><th id='MCnN8'></th></span></q></dt></tr></i><div id='MCnN8'><tfoot id='MCnN8'></tfoot><dl id='MCnN8'><fieldset id='MCnN8'></fieldset></dl></div>
                1. 下面是关于“JS中call apply bind函数手写实现demo”的攻略:

                  理解call、apply、bind函数

                  在手写这三个函数的过程中,我们必须先清楚地理解这三个函数的作用:

                  • call函数:调用一个函数,将一个对象作为第一个参数,以及多个参数传入该函数。
                  • apply函数:调用一个函数,将一个对象作为第一个参数,以及一个参数数组传入该函数。
                  • bind函数:创建一个新函数,该函数与原始函数具有相同的函数体,但this值被永久地绑定到了bind函数的第一个参数中。

                  手写call函数

                  call函数的实现过程如下:

                  Function.prototype.myCall = function (context) {
                    // 首先要获取调用call的函数,用this可以获取
                    const calledFunc = this
                  
                    // context为传入的需要调用函数的对象
                    context = context || window
                  
                    // 从arguments中获取到需要传递的参数
                    const args = [...arguments].slice(1)
                  
                    // 将该函数作为传入的对象的方法调用,以改变函数的this指针
                    context.fn = calledFunc
                  
                    // 最后把参数传入进去并执行
                    const result = context.fn(...args)
                  
                    // 删除对象上的方法
                    delete context.fn
                  
                    return result
                  }
                  

                  示例:

                  function sayName() {
                    console.log(this.name)
                  }
                  
                  const person = { name: 'Lucas' }
                  
                  sayName.myCall(person) // 'Lucas'
                  

                  手写apply函数

                  apply函数的实现过程如下:

                  Function.prototype.myApply = function (context) {
                    // 首先要获取调用apply的函数,用this可以获取
                    const calledFunc = this
                  
                    // context为传入的需要调用函数的对象
                    context = context || window
                  
                    // 从arguments中获取到需要传递的参数
                    const args = arguments[1]
                  
                    // 将该函数作为传入的对象的方法调用,以改变函数的this指针
                    context.fn = calledFunc
                  
                    // 最后把参数传入进去并执行
                    const result = context.fn(...args)
                  
                    // 删除对象上的方法
                    delete context.fn
                  
                    return result
                  }
                  

                  示例:

                  function sayName(age) {
                    console.log(`My name is ${this.name}, and I'm ${age} years old`)
                  }
                  
                  const person = { name: 'Lucas' }
                  
                  sayName.myApply(person, [25]) // 'My name is Lucas, and I'm 25 years old'
                  

                  手写bind函数

                  bind函数的实现过程如下:

                  Function.prototype.myBind = function (context) {
                    // 首先要获取调用bind的函数,用this可以获取
                    const calledFunc = this
                  
                    // context为传入的需要调用函数的对象
                    context = context || window
                  
                    // 从arguments中获取到需要传递的参数
                    const args = [...arguments].slice(1)
                  
                    // 返回一个新的函数,此时该函数的this指向已经绑定到了context上
                    return function () {
                      const newArgs = args.concat([...arguments])
                      return calledFunc.apply(context, newArgs)
                    }
                  }
                  

                  示例:

                  function sayName(age) {
                    console.log(`My name is ${this.name}, and I'm ${age} years old`)
                  }
                  
                  const person = { name: 'Lucas' }
                  
                  const sayNameWithPerson = sayName.myBind(person)
                  
                  sayNameWithPerson(25) // 'My name is Lucas, and I'm 25 years old'
                  

                  总结

                  通过手写call、apply、bind这三个函数,可以更深刻地理解它们的作用,并且掌握函数指针和this的相关知识点。在实际编码中,也可以更加灵活地应用这些函数,让代码实现更加简洁高效。

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  treetable.js没有checked做联动。于是自己基于treetable开发的一个小功能,希望能和大家一起交流一下。 1. 在当前HTML文档checked监听函数中增加以下代码 //联动 table.on('checkbox(quan_list)', function(obj){ //console.log(obj); //当前id var id = obj.
                  当使用Javascript的attachEvent来绑定事件时,我们希望能够给事件处理函数传递一些参数,但是attachEvent本身并不支持传递参数。下面介绍两种解决方法。
                  KnockoutJS是一款流行的JavaScript库,针对一个web应用程序的建立提供了比较好的基础架构。其中,表单的数据绑定功能是KnockoutJS最为常用的功能之一。本文将详细讲解KnockoutJS 3.x
                  下面是用javascript实现改善用户体验之alert提示效果的完整攻略。
                  在学习JavaScript编写贪吃蛇游戏之前,需要掌握以下的前置知识:
                  <i id='aWf0p'><tr id='aWf0p'><dt id='aWf0p'><q id='aWf0p'><span id='aWf0p'><b id='aWf0p'><form id='aWf0p'><ins id='aWf0p'></ins><ul id='aWf0p'></ul><sub id='aWf0p'></sub></form><legend id='aWf0p'></legend><bdo id='aWf0p'><pre id='aWf0p'><center id='aWf0p'></center></pre></bdo></b><th id='aWf0p'></th></span></q></dt></tr></i><div id='aWf0p'><tfoot id='aWf0p'></tfoot><dl id='aWf0p'><fieldset id='aWf0p'></fieldset></dl></div>
                  <legend id='aWf0p'><style id='aWf0p'><dir id='aWf0p'><q id='aWf0p'></q></dir></style></legend>
                  <tfoot id='aWf0p'></tfoot>
                    <tbody id='aWf0p'></tbody>

                          <small id='aWf0p'></small><noframes id='aWf0p'>

                            <bdo id='aWf0p'></bdo><ul id='aWf0p'></ul>