当前位置:网站首页>Function hoisting and variable hoisting
Function hoisting and variable hoisting
2022-08-02 03:52:00 【Xiaoru wants to sleep】
Let's look at two similar codes first
a = 2;var a ;console.log(a);console.log(a);var a = 2;The first output is 2
The second output value is undefined
You may be wondering why?
Let's look into it
This is because all declarations including variables and functions are processed first before any code is executed
So the first code actually handles this
var a;a = 2;console.log(a);The second code is handled like this
var a ;console.log(a);a = 2;This process is as if variable and function declarations are "moved" to the top from where they appear in the code.This process is called ascension
Note: Only the declaration itself will be hoisted, and assignments or other run logic will be left in place.If hoisting changes the order in which code is executed, it can cause very serious damage.Function declarations can be hoisted, but function expressions cannot be hoisted.
Function first
Both function declarations and variable declarations are hoisted, but in duplicated code, functions are hoisted first, then variables
foo();//1var foo;function foo() {console.log(1);}foo = function() {console.log(2);}This will output 1 instead of 2, this code will be understood by the engine as the following format
function foo(){console.log(1);}foo(); // 1foo = function(){console.log(2);}Here var foo although appears before the declaration of function foo()......, but because it is a duplicate declaration (and thus ignored) because when there is a duplicate declaration, the function declarationwill be hoisted before variable declarations.
边栏推荐
猜你喜欢
随机推荐
每日五道面试题总结 22/7/23
IP门禁:手把手教你用PHP实现一个IP防火墙
稳定好用的短连接生成平台,支持API批量生成
URL URL
js基础知识
js eventLoop 事件循环机制
你的本地创建的项目库还在手动创建远端代码仓库再推送吗,该用它了
Advanced gradient of skeleton effect, suitable for waiting for pictures
解决MySQL创建子视图并查看的时候,字符集报错问题
js 取字符串中某位置某特征的值,如华为(Huawei)=>华为
1.10今日学习
SQL分类、DQL(数据查询语言)、以及相应SQL查询语句演示
C语言 十六进制整数字符串转十进制整数
js 中this指向
display,visibility,opacity
---static page---
TypeScript 错误 error TS2469、error TS2731 解决办法
Dom实现input的焦点触发
uniapp | 官方提供的map组件使用问题
暴力方法求解(leetcode14)查找字符串数组中的最大公共前缀









