欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳細(xì)分析JavaScript函數(shù)定義

 更新時(shí)間:2015年07月16日 10:10:52   投稿:hebedich  
這篇文章主要給大家詳細(xì)分析了JavaScript函數(shù)定義的相關(guān)資料,需要的朋友可以參考下

函數(shù)

幾個(gè)要點(diǎn):

                a).函數(shù)是javascript中的一等公民 (重要性)
                b).函數(shù)是一個(gè)對(duì)象
                c).函數(shù)定義了一個(gè)獨(dú)立的變量作用域

定義方式

a)命名函數(shù):

          除非在另一個(gè)函數(shù)內(nèi)部定義,否則,命名函數(shù)是全局的。 

      // 全局的命名函數(shù)
  function add(x, y) {
    return x + y;
  }
  console.info(add(100, 200));  //300

b)匿名函數(shù):   

            匿名函數(shù)通常賦值給一個(gè)變量,再通過變量調(diào)用。

    var func = function (x, y) {
      return x + y;
    }
    console.info(func(5, 2)); //7

            匿名函數(shù)適用于以下這種 “立即執(zhí)行的匿名函數(shù)” 的情況:

    console.info(
      function (x, y) {
            return x + y;
          }(100, 200)  //立即調(diào)用
        );

C)定義方式影響代碼執(zhí)行效果

                命名函數(shù)可以先使用,再定義

    console.info(sum(10, 10));
    function sum(num1, num2) {
      return num1 + num2;
    }

              匿名函數(shù)必須先定義,再使用

    //console.info(sumFunc(10, 10));  //Uncaught TypeError: Property 'sumFunc' of object [object Object] is not a function 
    var sumFunc = function (num1, num2) {
      return num1 + num2;
    };
    console.info(sumFunc(10, 10));


函數(shù)返回值:

                 用return 生成返回值.如沒有return ,則函數(shù)返回undefined

 function func() {
 }
 console.info(func()); //undefined
 function func2() {
   return; //空的返回語句
 }
 console.info(func2()); //undefined

return里藏著的坑:

 var func = function (x, y) {
   var sum = x + y;
   return {
     value : sum
   }
 }

                   這么寫沒有問題:   調(diào)用 func(5,5)    返回的是 Object {value: 10}

                   然而: 

  var func = function (x, y) {
    var sum = x + y;
    return
    {
      value: sum
    };
  }
  console.info(func(5,5)); //undefined

                   return 后面跟著個(gè)回車換行的話,
                   調(diào)用 func(5,5)    顯示的是 undefined
                   編輯器幫我們?cè)趓eturn后加了個(gè)分號(hào);   然而在這情況下并沒有什么卵用。

函數(shù)即對(duì)象:

  function add(x, y) {
    return x + y;
  }
  console.info(add(100, 200)); //300
  var other = add; //other和add引用同一函數(shù)對(duì)象
  console.info(other(300, 400)); //700
  console.info(typeof other);  //function
  console.info(add === other); //true

嵌套定義的函數(shù):

                  在函數(shù)內(nèi)部,可以定義另一個(gè)函數(shù)。

  function outerFunc(a, b) {
    function innerFunc(x) {
      return x * x;
    }
    return Math.sqrt(innerFunc(a) + innerFunc(b));
  }
  console.info(outerFunc(3, 4)); //5

訪問外部變量:

             內(nèi)部函數(shù)可以訪問外部的變量與參數(shù)。

 var globalStr = 'globalStr';
 function outerFunc2(argu) {
   var localVar = 100;
   function innerFunc2() {
     localVar++;
     console.info(argu + ":" + localVar + ":" + globalStr);
   }
   innerFunc2(); //hello:101:globalStr
 }
 outerFunc2("hello");

返回函數(shù)的函數(shù):

               因?yàn)楹瘮?shù)是對(duì)象,所以可以作為返回值。

  function outerFunc(x) {
    var y = 100;
    return function innerFunc() {
      console.info(x + y);
    }
  }
  outerFunc(10)(); //110

以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。

相關(guān)文章

最新評(píng)論