call 和apply都是为了改变当前对象的this指针。传入第一个是一个对象也是this指针,第二个是参数。

<script>
   
var func=new function(){this.a="func"}
    
var myfunc=function(x){
        
var a="myfunc";
        alert(
this.a);
        alert(x);
    }
    myfunc.call(func,
"var");
</script>

func

var

 

 防止抖动js

  1. // 取自 UnderscoreJS 实用框架  
  2. function debounce(func, wait, immediate) {  
  3.     var timeout;  
  4.     return function() {  
  5.         var context = this, args = arguments;  
  6.         var later = function() {  
  7.             timeout = null;  
  8.             if (!immediate) func.apply(context, args);  
  9.         };  
  10.         var callNow = immediate && !timeout;  
  11.         clearTimeout(timeout);  
  12.         timeout = setTimeout(later, wait);  
  13.         if (callNow) func.apply(context, args);  
  14.     };  
  15.   }  
  16.   
  17. // 添加resize的回调函数,但是只允许它每300毫秒执行一次  
  18. window.addEventListener('resize', debounce(function(event) {  
  19.   
  20.     // 这里写resize过程  
  21.   
  22. }, 300));  

 

 posted on 2013-11-22 15:07  陈小胖胖blog  阅读(286)  评论(0)    收藏  举报