当前位置:网站首页>JS function promotion and declaration promotion of VaR variable
JS function promotion and declaration promotion of VaR variable
2022-07-06 12:13:00 【Aboci Bang】
Variable Promotion
// The first 1 topic ~~
var a = 1;
function bar() {
console.log(a);
if (!a) {
var a= 10;
}
console.log(a);
}
bar();
// ask : two console.log() What is the value of .
**// Output :**
undefined
10
// analysis :
Global variable has a, But there is also a function inside a So the global variable a Invalid inside function , because var Declared variables have no block level scope concept therefore if Inside a Promoted to the top of the function And only enhance the declaration part Do not promote the assignment part So the first one console Output the function's internal promotion a But there is no assignment at this point JS Will assign initial values to the variables of life undefined
the second console see if Conditions !a here a yes undefined by false that !a Namely true
Will enter if In scope to a assignment So the second one console Output 10.
There are several concepts to be clear about
var Functional scope But there is no block level scope
Block level scope : Including loop statement and loop body ; Braces for branch statements ; Or just a pair of braces .
Such as :
//(1)
if(true){
var a = 1;
}
//(2)
for(var i=0;i<10;i++){
var a = 1;
}
//(3)
{
var a = 1;
}
All of the above will make var Declared variable promotion .
// The first 2 topic ~~
function fun5(a, b) {
var a;
var b;
console.log(a, b);
}
fun5(1, 2);
~~ Output
1 2
// analysis :
It's stated here a,b In fact, the program automatically declares it to us That is to say, the formal parameter variable and the variable we declare inside the function and do not assign a value are one variable ( The premise is that the variable names should be the same )
Formal parameter variables do not write declarations js It will also help us implicitly declare Artificial manual declaration is actually optional , Mainly to confuse us …
// The first 3 topic ~~
function fun() {
var n1 = n2 = n3 = 1;
}
fun();
console.log(n2, n3);
// Output :
1 1
// analysis :
Here we need to pay attention to :
(1) Not inside the function var Variables declared by keywords are treated as global variables , therefore n2 n3 yes Global variables .
var Next to the back n1 Is the declared local variable .
(2) Assignment operator execution order
n3 = 1; // n3 yes 1
n2 = n3; // n2 yes 1
var n1 = n2; // because n1 Is a local variable, so an external call will show undefined .
Function enhancement
c();
var c=2;
function c(){
console.log(5);
}
c();
answer :
Function enhancement : Before the program is executed , It will promote the function as a whole to the front of the scope , You can call first and then create .
therefore : First line c() Yes, you can call function c Of .
Be careful : The variable name is the same as the function name , Because the function name It is essentially a variable , Saved the function . So the function c It can be written.
var c = function(){
console.log(5);
}
// So the next sentence Namely
var c = 2;
// here c yes 2, It's no longer a method . So the display function is undefined .
a();
function a() {
console.log(111);
}
a();
function a() {
console.log(222);
}
a = function () {
console.log(333); }
a();
The result is :222 222 333
explain : Function expression No promotion ! Because the equal sign is assignment It's not a statement
~~function The first one will improve
a = function () {
console.log(333); }
a();
function a() {
console.log(222);
}
a();

function a() Promoted to the front Function expressions do not promote Then be a=function() covers
var a = 5;
function a() {
console.log(111);
}
console.log('a :>> ', a);

amount to 
边栏推荐
- vim命令行笔记
- Who says that PT online schema change does not lock the table, or deadlock
- Knowledge summary of request
- Arduino uno R3 register writing method (1) -- pin level state change
- Learning notes of JS variable scope and function
- Oppo vooc fast charging circuit and protocol
- arduino获取数组的长度
- Basic operations of databases and tables ----- view data tables
- Pat 1097 duplication on a linked list (25 points)
- JS 函数提升和var变量的声明提升
猜你喜欢

高通&MTK&麒麟 手機平臺USB3.0方案對比

记一次云服务器被密码爆破的经历——关小黑屋、改密码、改端口

JS变量类型以及常用类型转换

Arm pc=pc+8 is the most understandable explanation

Fashion-Gen: The Generative Fashion Dataset and Challenge 论文解读&数据集介绍

Stm32f1+bc20+mqtt+freertos system is connected to Alibaba cloud to transmit temperature and humidity and control LED lights

小天才电话手表 Z3工作原理

程序员老鸟都会搞错的问题 C语言基础 指针和数组

Basic use of pytest

Machine learning -- decision tree (sklearn)
随机推荐
Vscode basic configuration
Arduino JSON data information parsing
MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
Symbolic representation of functions in deep learning papers
Classification, understanding and application of common methods of JS array
选择法排序与冒泡法排序【C语言】
Understanding of AMBA, AHB, APB and Axi
JS 函数提升和var变量的声明提升
Pytorch-温度预测
Raspberry pie tap switch button to use
基於Redis的分布式ID生成器
小天才电话手表 Z3工作原理
记一次云服务器被密码爆破的经历——关小黑屋、改密码、改端口
Imgcat usage experience
Redis 缓存更新策略,缓存穿透、雪崩、击穿问题
Apprentissage automatique - - régression linéaire (sklearn)
.elf .map .list .hex文件
程序员老鸟都会搞错的问题 C语言基础 指针和数组
数据分析之缺失值填充(重点讲解多重插值法Miceforest)
Redis based distributed ID generator