JavaScript中定义函数的几种方式?

函数的组成:函数名 + 函数体

1、使用function关键字定义函数 -- 具有优先级,优先将function关键字定义的函数优先执行

  function functionName(arg0, arg1 ,..., argN){

      statements

  }

  函数的调用:functionName()

2、使用函数表达式的形式定义函数(即将匿名函数复制给变量)

  var variable = function(arg0, arg1 ,..., argN){

    statements

   }

  console.log(typeof variable); //function

  函数调用:variable();

3、使用new Function构造函数定义函数

  var variable = new Function('name','alert("hello,"+name)'); //最末尾的是函数体,其前面的都是参数

  console.log(typeof variable); //function

  函数调用:variable('world');

注意:

(1)使用fucntion关键字定义的函数,函数一旦声明,允许任意调用(在函数定义前、函数定义后、函数内部,可以在任意位置调用)

(2)使用函数表达式、new Function构造函数定义的函数,不能在函数定义前使用

函数的参数:

形参:函数定义时所带参数

实参:函数调用时所带参数