zoukankan      html  css  js  c++  java
  • JavaScript中函数function fun(){}和 var fun=function(){}的区别

    function fun(){} 和 var fun=function(){}的区别

    标题有点长····

    废话少说,其实他们的主要区别就是“函数声明的提前行为”.

    var fun=function(){
        alert("hello world!");
    };
    fun();       //hello world!
    
    /**********************************/
    
    function fun() {
        alert("hello world!");
    }
    fun();       //hello world!

     正常情况下两种方式都会进行正常的编译,并输出“hello world!”,下面把函数调用放在上面再测一下。

    fun();       //报错
    var fun=function(){
      alert("hello world!");
    };
    /**********************************/
    fun();       //hello world!
    function fun() {
      alert(
    "hello world!");
    } 

     前者不会被提升,后者被提升到“顶部”

    最近在维护一个前端交流群,群内有各个领域的大佬,各路妹子,程序员鼓励师等你来撩,欢迎加入,群号:249620372
  • 相关阅读:
    软件课程设计(3)
    软件课程设计(2)
    软件课程设计(1)
    继往开来第五天
    勤勤恳恳第四天
    撸起袖子第三天
    厉兵秣马第二天
    项目初定第一天
    Magic-Club第五天
    Magic-Club第四天
  • 原文地址:https://www.cnblogs.com/wangyue99599/p/7347201.html
Copyright © 2011-2022 走看看