当前位置:网站首页>TypeScript:var
TypeScript:var
2022-07-01 14:47:00 【Everything has changed every day】
var Only variables defined Global scope and Function scope , There is no block scope .
Variables defined in a function belong to the scope of the function , The rest are global scopes .
Use var Defined variables can also be accessed before definition , The value is undfiend, This phenomenon is called Variable Promotion .
var a = 1;// Defined outside the function , Belongs to the global scope , Globally accessible
function f()
{
console.log(b);// Variable Promotion , Output undefined
if(a == 1)// Variables with global scope can be accessed directly in the function
{
var b = 2;// Defined in a block within a function , Belongs to the function scope , You can access... Only in functions
}
console.log(b);// Access to b, Output 2
}
f();
console.log(b);// Cannot access b
Use var You can define the same variables repeatedly in the same scope , No mistake. :
var a = 1;
var a = "hello";
console.log(a);// Output hello
Use var Variables defined in functions , If the variable name is the same as that of the global scope , It will mask the visibility of global variables within the scope of the function :
var a = 1;
function f()
{
var a = "hello";// Defined within the scope of the function a, Will shield the global scope a The visibility of , Yes a The access of only affects a
console.log(a);// Within the scope of the output function a,hello
}
f();
console.log(a);// Output a, The value is 1
Because there is no block scope , Outside the scope of function for,while Variables defined in statement blocks such as are also considered as global scopes :
for(var a = 0, c=""; a < 10; a++) {// It is equivalent to defining i and c
var b = "";// Each cycle is redefined b
b += (a + " ");
c += (a + " ");
}
console.log(a); // You can access i, Output :10
console.log(b); // Because each cycle is redefined b, So the output :9
console.log(c); // You can access c, Output :0 1 2 3 4 5 6 7 8 9
边栏推荐
- Provincial election + noi Part 10 probability statistics and polynomials
- Basis of target detection (NMS)
- JVM第一话 -- JVM入门详解以及运行时数据区分析
- The first word of JVM -- detailed introduction to JVM and analysis of runtime data area
- Internet hospital system source code hospital applet source code smart hospital source code online consultation system source code
- 定了!2022海南二级造价工程师考试时间确定!报名通道已开启!
- sqlilabs less10
- 适合没口才的人做,加入中视频伙伴计划收益是真香,一个视频拿3份收益
- [zero basic IOT pwn] reproduce Netgear wnap320 rce
- One of the data Lake series | you must love to read the history of minimalist data platforms, from data warehouse, data lake to Lake warehouse
猜你喜欢
JVM second conversation -- JVM memory model and garbage collection
Semiconductor foundation of binary realization principle
Cannot link redis when redis is enabled
如何看待国企纷纷卸载微软Office改用金山WPS?
Word2vec yyds dry goods inventory
sqlilabs less-11~12
Don't want to knock the code? Here comes the chance
建立自己的网站(14)
Today, with the popularity of micro services, how does service mesh exist?
【牛客网刷题系列 之 Verilog快速入门】~ 使用函数实现数据大小端转换
随机推荐
基于价值量化的需求优先级排序方法
C 语言基础
Pat 1065 a+b and C (64bit) (20 points) (16 points)
Chapter 4 of getting started with MySQL: creation, modification and deletion of data tables
Word2vec yyds dry goods inventory
数字化转型:数据可视化赋能销售管理
tensorflow2-savedmodel convert to pb(frozen_graph)
Take you to API development by hand
Research Report on the development trend and competitive strategy of the global indexable milling cutter industry
Is the futures company found on Baidu safe? How do futures companies determine the regularity
sqlilabs less10
Research Report on the development trend and competitive strategy of the global pipeline robot inspection camera industry
C learning notes (5) class and inheritance
NPDP能给产品经理带来什么价值?你都知道了吗?
Basis of target detection (NMS)
从零开发小程序和公众号【第三期】
Build your own website (14)
Provincial election + noi Part XI others
数据湖系列之一 | 你一定爱读的极简数据平台史,从数据仓库、数据湖到湖仓一体
2022-2-15 learning xiangniuke project - Section 4 business management