您的位置:首页 > 移动开发

js中的装饰器。拦截器。细细品味 bind apply call

2017-03-13 17:40 246 查看
var zlw = {

    name: "zlw",

    sayHello: function (age) {

         console.log("hello, i am ", this.name + " " + age +" years old");

     }

};

var  xlj = {

    name: "xlj",

};

zlw.sayHello(24);

VM273:4 hello, i am  zlw 24 years old

undefined
zlw.sayHello.bind(xlj, 24)()
VM273:4 hello, i am  xlj 24 years old

zlw.sayHello.call(xlj, 24);// hello, i am xlj 24 years old
zlw.sayHello.apply(xlj, [24])


function debounce(fn, delay) {
 // 维护一个 timer
 let timer = null;
 // 能访问 timer 的闭包
 return function() {
   // 通过 ‘this’ 和 ‘arguments’ 获取函数的作用域和变量
   let context = this;
   let args = arguments;
   // 如果事件被调用,清除 timer 然后重新设置 timer
   clearTimeout(timer);
   timer = setTimeout(function() {
     fn.apply(context, args);
   }, delay);
 }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: