- 考点: 变量声明和函数定义提升, 函数优先级高
- 考点: 函数定义提升, 已有函数可被覆盖
注释: x = 2; x = x + 2; 会返回 4。
函数 function add(x) { return (x = x + 3); } 等价于 function plus(x) {x = x + 3; return x; }
变量提升:JavaScript代码是按顺序执行的吗?
极客-浏览器原理 https://time.geekbang.org/column/article/119046
什么是变量提升
变量提升, 指在js代码执行过程中(解释性语言:预编译+执行), 变量和函数的声明提升到代码开头, 此时的变量默认🈯️为undefined
JavaScript 代码的执行流程
相同的变量或者函数怎么办?
showName()var showName = function() {console.log(2)}function showName() {console.log(1)}// 1
函数提升要比变量提升的优先级要高一些,且不会被变量声明覆盖,但是会被变量赋值之后覆盖
showName()var showName = function() {console.log(2)}showName()function showName() {console.log(1)}showName()// 1// 2// 2
