Giter VIP home page Giter VIP logo

Comments (4)

erasec1994 avatar erasec1994 commented on June 23, 2024 2

将函数名当作 “变量名”。会好理解很多。如下:

其实 function add (){} 这样声明一个函数 类似 语法糖。

将 function add (){} 转化成 var add = function (){} //先声明,后使用。这样会好理解。

所以我认为 function add (){}是一种语法糖。

不过 function add (){} 和 var add = function (){} 是存在区别的,区别在于 函数声明提升 和 变量声明提

升。(使用代码来表达):

<script>
say()
function say(){
   console.log(0);
}
// 函数声明提升
这段代码中,无论执行语句在 function say (){}的前面还是后面,都能正常执行。
</script>
<script>
say2()
var say2 = function (){
   console.log(0);
}
// 变量声明提升
这段代码中,因为使用的是 变量的声明方式来声明一个函数,
在 JS 中 var 声明的变量只会在到该变量的赋值语句的时候才会生效,
所以执行语句必须放在 var say2 = function (){} 后面,否则会抛出一个错误。
</script>

也就是 当使用 函数声明 重复声明 两个函数的时候,因为函数声明提升,会先提取到程序开始的前面,当

重复声明的时候,会取最后声明的那个函数。所以当执行函数的时候,会使用后声明的那个函数而不是先

声明的函数。

//貌似说复杂了,还没说清楚的感觉。。。

from frontend-interview.

zhangolve avatar zhangolve commented on June 23, 2024

重复生命两个函数跟重复生命两个变量本质上是一样的,都是以最后一次声明为准。

var a=5;
var a=6;
console.log(a); //  6

改改顺序方便理解:

<script> 
    var m= 1, j = k = 0; 
    function add(n) { 
        return n = n+1; 
  } 
  
    function add(n) { 
        return n = n + 3; 
    } 
  y = add(m);   //4
z = add(m);     //4
</script> 

from frontend-interview.

facejiong avatar facejiong commented on June 23, 2024

函数是对象,函数名相当于name属性,重复声明后者会覆盖前者

from frontend-interview.

C1erman avatar C1erman commented on June 23, 2024

在支持Function.name属性的浏览器中,name便代表着具名函数的函数名,可知就是function对象的一个属性,对象是引用传递,重复声明便会指向新的物理空间。

from frontend-interview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.