JS函数声明和函数表达式的区别

 // 函数声明
    function funDeclaration(type){
        return type==="Declaration";
}
// 函数表达式
 var funExpression = function(type){
     return type==="Expression";
 }
1.Javascript 中函数声明和函数表达式是存在区别的,函数声明在JS解析时进行函数提升,因此在同一个作用域内,不管函数声明在哪里定义,该函数都可以进行调用。而函数表达式的值是在JS运行时确定,并且在表达式赋值完成后,该函数才能调用。
2.函数声明可以先调用再声明,而函数表达式必须先定义再调用

 

写一个demo,你们猜一下结果:

   function test() {
        alert(22222)
    }

    var test = function(){
        alert(11111)
    }

    test(); // ???

 

或者

    var test = function(){
        alert(11111)
    }

    function test() {
        alert(22222)
    }

    test(); // ???

 

posted @ 2020-04-04 21:29  骑着小毛驴1688  阅读(600)  评论(0编辑  收藏  举报