zoukankan      html  css  js  c++  java
  • 对call的理解

    f.call(o,para1,para2,....);

    f: 是调用的函数

    o:是函数f中this指向的对象

    para1,para2,...:是调用 f 函数时传入的实参

     eg:

        function Father(name){
          this.name = name || '名字'     
        }
        function Son(){};
        let son = new Son();
        Father.call(son)
        console.log(son.name) //名字
        console.log(Son.name) //undefined
       
    

     Father.call(son)中call的作用是,调用Father函数,使Father函数中的this指向为son,去执行函数;也就是改变了Father函数中this的指向。

    f.call(obj,params1,params2) === f.bind(obj,params1,params2)()

    call和bind 的区别:  call 和applay 都是调用后立即执行的 ,bind调用之后返回的是原函数需要再调用一次

    防抖函数debounce:

    function debounce(fn,time){
        return funtion(){
            timer&&cleanInterval(timer)
            var timer = setTimeout(()=>{
                fn.applay(this,argument);
            },time) ;
        }          
    }
      

     节流函数 throttle:

    function thorotte(fn,time){
        var lastTime = 0;
        return function(){
           if(new Date().getTime() - lastTime >time){
               fn.apply(this,argument)
               lastTime = new Date().getTime()
           }
        }  
    }    
  • 相关阅读:
    关于php中trim、ltrim和rtrim
    文件读取
    字典
    列表
    数据类型作业
    常用的数据类型
    字符串
    编码
    MVC 入门
    JavaScript 类型浅解
  • 原文地址:https://www.cnblogs.com/fewhj/p/10371327.html
Copyright © 2011-2022 走看看