zoukankan      html  css  js  c++  java
  • 函数_回调函数

    1,什么是函数?

      *实现特定功能的N条语句的封装题

      *只有函数是可执行的,其他类型的数据不能执行

    2,为什么要用函数?

      *提高代码复用

      *便于交流 

    3,如何定义函数?

      *函数声明

      *表达式

     function fn1() { // 函数声明
        console.log('fn1');
      } 
    
      var fn2 = function() { // 表达式
        console.log('fn2');
      }

    4
    ,如何执行一个函数? *test():直接调用 *obj.test():通过对象调用 *new test():new调用 *test.call/apply(obj): 临时让test 成为obj的方法进行调用
     var obj = {};
    
    function test2 () {
    
    this.xxx = '999';
    
    }
    
    //obj.test2()  // 不能直接调用
    
    test2(obj) // 相当于obj.test2()
    
    console.log(obj.xxx); //  999

    5,什么函数才是回调函数?

      *你定义的

      *你没有调

      *但最终它执行了

    6,常见的回调函数?

      *dom函数回调函数

      *定时器回调函数

      *ajax请求函数

      *生命周期回调函数

    document.getElementById('btn').onclick = function() { // dom事件回调函数
    
    alert(this.innerHtml);
    
    }
    
    
    //定时器:又分为
      *超时定时器
      *循环定时器
    
    setTimeout(function() { // 定时器回调函数
    
    alert('结束');
    
    }, 2000);
  • 相关阅读:
    Codeforces Round #360 B
    Codeforces Round #360 C
    Codeforces Round #360 D
    新姿势 树剖求LCA
    Codeforces 165D Beard Graph 边权树剖+树状数组
    hdu3966 树链剖分+线段树 裸题
    Codeforces Round #425 D
    Codeforces Round #425 B
    Codeforces Round #425 A
    bzoj 1036 树链剖分+线段树 裸题
  • 原文地址:https://www.cnblogs.com/zhangyezi/p/13599250.html
Copyright © 2011-2022 走看看