当前位置:网站首页>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.
边栏推荐
猜你喜欢

Shanghai Pudong Development Bank Software Test interview real question

Michael Wooldridge, professeur à l'Université d'Oxford: comment la communauté de l'IA voit les réseaux neuronaux depuis près de 40 ans

新工作第一天

浦发银行软件测试面试真题(小编面试亲测)

Graphic system - 1 Layout loading

业务层修改--根据现有框架的反推修改

About Covariance and Correlation(协方差和相关)

How to manage interface documents efficiently and gracefully

Professor Michael Wooldridge of Oxford University: how the AI community views neural networks in the past 40 years

向上转型和向下转型
随机推荐
内存泄露
Graphic system - 2 View drawing
618活动季——百数低代码平台特享折扣来临
基于固态激光雷达辅助的十六线机械雷达和单目相机的外参标定方法
Yixin Huachen: real estate enterprises want to grasp the opportunity of the times for digital transformation
【C#】详解值类型和引用类型区别
openGauss内核:SQL解析过程分析
AOSP清华镜像下载错误解决
Graphic system - 1 Layout loading
【软件测试】2022年普通高等学校招生全国统一考试
OOM out of memory 内存溢出
select/poll/epoll
堆的概念和代码实现
C# 41. Int to string
声网发布灵隼物联网云平台 可一小时构建示例场景
Sword finger offer 11 Minimum number of rotation array
打破学科之间壁垒的STEAM教育
亿信华辰:地产企业数字化转型想要把握时代机遇
postgresql数据库docker
Can I open an account today and buy shares today? Is it safe to open an account online?