当前位置:网站首页>Anonymous function this pointing and variable promotion
Anonymous function this pointing and variable promotion
2022-06-28 18:47:00 【Wood without melon】
Reference article :https://www.jb51.net/article/161019.htm
var a = 10;
(function() {
console.log(a)
a = 5
console.log(window.a)
var a = 20;
console.log(a)
})()
analysis : In the immediate function ,var a = 20; Statement defines a local variable a, because js Variable declaration promotion mechanism , local variable a The declaration of is promoted to the top of the body of the function that executes the function immediately , And since such promotion does not include assignment , So the first print statement prints undefined, The last statement will print 20.
Due to variable declaration Promotion ,a = 5; When this statement is executed , Local variables a Already declared , however The execution of anonymous functions is global , Therefore, its effect is on local variables a assignment , here window.a It is still the first assignment 10.
var name = 'window'
var person = {
name: 'Alan',
sayOne: function() {
console.log(this.name)
},
sayTwo: function() {
return function() {
console.log(this.name)
}
}
}
person.sayOne() //Alan
person.sayTwo()() // window- intra-function this Point to the caller
- sayOne The caller is person object , therefore this Point to person;
- sayTwo Although the caller of is also person object , But the difference is that this call doesn't type this Instead, an anonymous function is returned globally
- This anonymous function is not called and executed as a method of an object , Is performed globally
Make some changes
var name = 'window'
var person = {
name :'Alan',
sayName:function () {
var that = this
return function () {
console.log(that.name)
}
}
}
person.sayName()() // Alanextend
【 Variable Promotion 】 What is the output of the following code ?
function sayHi() {
console.log(name);
console.log(age);
var name = "Lydia";
let age = 21;
}
sayHi();- A:
Lydiaandundefined - B:
LydiaandReferenceError - C:
ReferenceErrorand21 - D:
undefinedandReferenceError
answer : D
In the function , We use var The keyword declares name Variable . This means that the variable will be promoted during the creation phase (JavaScript Memory space will be allocated to the variable during the creation phase ), The default value is undefined, Until we actually execute to the line using the variable . We haven't name Variable assignment , So it still remains undefined Value .
Use let keyword ( and const) Declared variables also have variable promotion , But with var Different , Initialization is not promoted . In our statement ( initialization ) Before they , They are inaccessible . This is known as “ A temporary dead zone ”. When we try to access a variable before declaring it ,JavaScript Will throw out a ReferenceError.
边栏推荐
- SMARCA2抗体研究:Abnova SMARCA2 单克隆抗体方案
- Tensorboard Usage Summary
- Opencv中使用Tracker实现物体跟踪
- Analysis of the core components of mybayis
- 使用.NetCore自带的后台作业,出入队简单模拟生产者消费者处理请求响应的数据
- Upload file list (repeated file names are marked with brackets)
- 牛津大学教授Michael Wooldridge:AI社区近40年如何看待神经网络
- 【Unity3D】相机跟随
- 注意!PMP紧急缓考今天就截止了!
- 基于管线的混合渲染
猜你喜欢
随机推荐
idea其他分支合并到dev分支
An in-depth analysis of the election mechanism in kubernetes
【软件测试】2022年普通高等学校招生全国统一考试
业务层修改--根据现有框架的反推修改
Can I open an account today and buy shares today? Is it safe to open an account online?
CVPR2022 | 浙大、蚂蚁集团提出基于标签关系树的层级残差多粒度分类网络,建模多粒度标签间的层级知识
C# 41. int与string互转
Is it reliable to open an account for the shares of Oriental Wealth software? Where is safe to open an account
EasyCVR新建用户后,视频调阅页面不能点击的问题修复
Upload file list (repeated file names are marked with brackets)
数据资产为王,如何解析企业数字化转型与数据资产管理的关系?
tensorboard 使用总结
Shell脚本批量修改文件目录权限
图形系统——2.view绘制
async-validator.js数据校验器
声网 VQA:将实时互动中未知的视频画质用户主观体验变可知
基于管线的混合渲染
Object tracking using tracker in opencv
Record an emotet Trojan horse handling case
解析机器人主持教学的实践发展









