函数表达式和函数声明

来源:互联网 发布:大唐绍兴江滨热电知乎 编辑:程序博客网 时间:2024/04/24 19:33
函数表达式和函数声明

函数声明举例:

function shenlu(type){

   return type==="decloartion";

}

解释:在js解析时,进行函数提升,因此在同一个作用域内,不管函数声明在哪里定义,该函数都可以进行调用。

Eg:

shenlu("decloartion");//ture

function shenlu(type){

   return type==="decloartion";

}

因为变量提升后的样式为:

function shenlu(type){

   return type==="decloartion";

}

shenlu("decloartion");

函数表达式举例:

var shenlu=funciton(type){

   return type==="expression";

}

解释:函数表达式的值在js运行时才确定,并且在表达式完成后,该函数才能调用。

Eg:

shenlu("Expression");//false

var shenlu=funciton(type){

   return type==="expression";

}

 

提升后:

var shenlu;

shenlu("Expression");

shenlu=function(type){

    return type==="expression";

}

原创粉丝点击