Comments (4)
将函数名当作 “变量名”。会好理解很多。如下:
其实 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.
重复生命两个函数跟重复生命两个变量本质上是一样的,都是以最后一次声明为准。
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.
函数是对象,函数名相当于name属性,重复声明后者会覆盖前者
from frontend-interview.
在支持Function.name属性的浏览器中,name便代表着具名函数的函数名,可知就是function对象的一个属性,对象是引用传递,重复声明便会指向新的物理空间。
from frontend-interview.
Related Issues (20)
- JS 作用域链相关问题 HOT 5
- JS 作用域、原型链 HOT 3
- ES5中“严格模式”与“非严格模式”的区别? HOT 3
- ajax请求序列化问题 HOT 5
- 循环/闭包/setTimeout/Promise 综合 HOT 1
- 服务端如何区分不同的用户 HOT 4
- 使用ES6实现该方法 HOT 13
- dom操作相关 HOT 7
- 什么是FP?与OOP的关系?
- 为何会出现浏览器兼容性问题?如何解决? HOT 2
- 前端性能优化 HOT 2
- JavaScript 分组 HOT 13
- 自适应和响应式有什么区别和联系
- 如何实现深拷贝? HOT 1
- 两个js数组相关题目 HOT 7
- 面试题汇总
- jrg interview questions HOT 7
- 实现一个深拷贝 HOT 8
- 宏任务微任务题 HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from frontend-interview.