一、默认赋值

  • 默认赋值:预先给函数的参数一个值
    1. <script>
    2. function go(type="get"){
    3. console.log(type)
    4. }
    5. go()
    6. go("post")
    7. </script>

    二、回调函数

  1. <script>
  2. /* 回调函数:就是将函数作为参数传递给另外一个函数
  3. 作用:可以获取函数内部的值 */
  4. function http(callback){
  5. var data = {"name":"cheng",age:18};
  6. callback(data);
  7. }
  8. function hanleData(res){
  9. console.log(res)
  10. }
  11. http(hanleData);
  12. </script>

三、箭头函数

  • 参数只有一个可以不用小括号,输出语句只有一行可以不用大括号
    1. <script>
    2. var a = function(x){
    3. console.log(x)
    4. }
    5. var a = x=>{
    6. console.log(x)
    7. }
    8. var a = (x,y)=>{
    9. console.log(x)
    10. }
    11. </script>
  1. <script>
  2. function show(x){
  3. return x;
  4. }
  5. /* */
  6. var go=x=>x;
  7. var test = z=>console.log(z);
  8. var getInfo=(x,y)=>{
  9. console.log(x);
  10. console.log(x+y)
  11. }
  12. console.log(go(10))
  13. test(20)
  14. </script>

箭头函数的好处

解决函数内部this关键字的指向问题
当函数直接调用时,this指向window

  1. <div id="test">hello world</div>
  2. <script>
  3. /* */
  4. var test = document.getElementById("test");
  5. test.onclick = function(){
  6. setTimeout = (function(){
  7. console.log(this)
  8. },300)
  9. }
  10. // test.onclick = function(){
  11. // console.log(this)
  12. // go()
  13. // }
  14. // function go(){
  15. // console.log(this)
  16. // }
  17. // test.onclick = go;
  18. </script>

四、构造函数

  1. <script>
  2. /* 不要使用 */
  3. var go = new Function('a','b','alert(a*b)')
  4. go(4,5)
  5. </script>

五、返回值 retrun

  • 返回值就是函数的执行结果,js中函数可以没有返回值
  • 使用return 语句后,返回值后面的语句不会执行

    1. <script>
    2. /* */
    3. function show(){
    4. return "hello world"
    5. console.log(3)
    6. }
    7. console.log(show())
    8. function og(x){
    9. console.log(x)
    10. }
    11. </script>

    六、函数的参数

  • 函数传不定参

    1. 函数内部有个arguments对象,接收函数传递过来的参数<br /> 是一个类数组对象
    <script>
          /* 函数传不定参 
          函数内部有个arguments对象,接收函数传递过来的参数
          是一个类数组对象*/
          function go(a,b,c){
              console.log(a)
              console.log(a+b)
              console.log(a+b+c)
          }
          go(10,10,30,50)
      </script>
    

    七、重载

  • 重载:根据传入参数不一样,动态决定调用哪一种方法

  • 原因:js不支持重载,重复声明,覆盖掉了

     可以使用arguments对象模拟重载
    
    <script>
          /* 
           */    
          function go(a,b){
              console.log(a+b)
          }
          function go(a){
              console.log(a)
          }
          function show(){
              if(arguments.length == 2){
                  console.log(arguments[0]+arguments[1])
              }else if(arguments.length == 1){
                  console.log(arguments[0])
              }
          }
          show(20)
          show(20,30)
      </script>
    

    八、改变this

  • Javascript的函数也是对象,它有三种方法可以改变函数内部this关键字的指向。

1.bind
2.call
3.apply()

8-1、bind

bind()改变了函数执行的上文环境,不会马上被执行

<script>
        /* 改变函数内部的this关键字的指向 */
        var name = "cheng";
        var obj = {
            name :"li"
        }
        /* bind()改变了函数执行的上文环境 */  
        var test = function(){
            console.log(this.name)
        }.bind(obj);
        test() 
    </script>

8-2、call

  • 马上执行,一个一个的传参

    call(thisObj,params)
    //第一个参数指向函数内部的this,之后的参数是需要被传入函数的参数
    
  • 功能

1.改变函数内部this关键字的指向
2.可以让对象拥有一个它没有的方法

 <script>
        var li = "li";
        var obj = {
            name:"cheng"
        }  
        var test = function(){
            console.log(this.name)
        } 
        test.call(obj) 
    </script>

8-3、apply()

  • 马上执行,传进去是数组
    //apply(thisObj,[params])
     var person = {
             name : 'cheng',
             sayName(label){
                 console.log(label+":"+this.name);
             }
         }
         var jiang = {
             name:"jiang"
         }
         person.sayName.call(jiang,["jiang"])