当前位置:网站首页>Problems related to self calling functions and automatically defining var because variables are not declared
Problems related to self calling functions and automatically defining var because variables are not declared
2022-07-16 05:40:00 【Genting, Incubus】
First, give the default undeclared variables var Theorem :
Function body , Do not declare variables ( Such as a=2) He will look up the definition of this variable from his superior , When you find the top layer , If not defined , It will be used automatically var Definition ( Non strict mode )
And under the self calling function It has and not only has the following characteristics :
- Function declaration and execution are continuous Together. , Both No function declaration promotion
- From within the calling function The definition of a function is Constant
Go straight to the code Use topics to consolidate
For self calling functions
// Function case 1
var a=1;
var a=2;
(function a() {
console.log(a);// Output a Function content Because there is no function declaration promotion
})()
console.log(a);//2 a Functions are declared and defined in self calling functions It's not going out and var Repeat statement 2 covers 1
// Function case 2
var a=1;
(function a() {
a=2; // Because there are a function Inconformity theorem Can't var But in the self calling function a Is a constant Constants cannot be modified here a=2 Is an invalid operation Report in strict mode const error
console.log(a);// Output a Function content
})()
console.log(a);//1 There is no trigger theorem No, var So from the calling function a Your statement has not been circulated
// Function case 3
(function b() {
a=2; // There is no a Coincidence theorem therefore var a=2
console.log(a);// Output 2
})()
console.log(a);//2 Coincidence theorem therefore var a=2 Passed outside the calling function Function case 1 in , Because the function declaration is not promoted , So it won't show up a=2 hold a Replace function Lead to “ primary a function ” Can't be called .
Function case 2 in , Because there are a function Inconformity theorem Can't var But in the calling function function a Is a constant Constants cannot be modified here a=2 Is an invalid operation Report in strict mode const error
Function case 3 in , It conforms to the theorem , So it's equivalent to the outermost layer There is one var a=2 So it comes out of the function body
For ordinary functions
// Function case 4 Under normal circumstances
function a(){
var a=2 //a=2 Is defined in the function body It's not going out
console.log(a)//2
}
a()
console.log(a)//a Function content
// Function case 5
function a(){
a=2// Compare the function 2 There are a function Inconformity theorem Can't var a Function is not constant a=2 Not defined in a function therefore a=2 covers a function So the effect is almost equal to the function case 6
console.log(a)//2
}
a()
console.log(a)//2
// Function case 6
function a(){}
a=2// here a The value is replaced with 2
console.log(a)//2
console.log(a)//2It's mainly here Function case 5 To compare , contrast Function case 2, There are a function Inconformity theorem Can't var,a Function is not constant And a=2 Not defined in a function therefore a=2 Find the outer layer defined a Function and covers a function So the effect is almost equal to Function case 6
边栏推荐
猜你喜欢
随机推荐
Cross domain exceptions where the admin system is nested in a third-party system
【黄啊码】PHP配合xlswriter实现无限表头层级Excel导出
Svelte official introductory tutorial (3) -- props
js--笔试题(集)
2022第十五届全国大学生信息安全竞赛(ciscn)西南赛区部分WP
反射获取成员方法和成员变量
Single file component
[Huang ah code] PHP realizes file download and supports continuous transmission at breakpoints
ES6--Set
【黄啊码】为什么我建议您选择go,而不选择php?
【黄啊码】redis实现模糊查询并删除|redis根据前缀获取key
Svelte 官方入门教程(4)—— 模板逻辑
[Huang ah code] Microsoft Internet Explorer will be retired. Netizens said: what should I do in the future?
[Huang ah code] fastadmin accesses wechat payment and Alipay payment
Free CDN jsdelivr acceleration website
【黄啊码】今天居然有人问我:where 1=1 是什么意思?
admin 系统被嵌套在第三方系统中的跨域异常
【prettier】通过prettier自动格式化代码不生效
fastadmin的二次开发教程【简单搭建、多表格问题,API开发】
查找树形数据的某个值


![[Huang ah code] wechat applet +php realizes instant messaging chat function](/img/c4/d571f10588b7b4679105f9552bb727.png)





