The following editor will bring you a summary of this pointing based on javaScript. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.
The pointing of this in JavaScript has always been a worry for front-end colleagues, and it is also the first choice for interview questions. Now let’s summarize the pointing of this in js. First, you need to understand a few concepts:
1: Global variables are mounted under the window object by default
2 : Generally, this points to its caller
3: In the arrow function of es6, this points to the creator, not the caller
4: You can change the pointer of this through call, apply, and bind
Let’s analyze it in detail
1: When calling the function
## (non-strict mode)
const func = function () { console.log(this); const func2 = function () { console.log(this); }; func2(); //Window }; func(); //Window
(strict mode)
'use strict' const func = function () { console.log(this); const func2 = function () { console.log(this); }; func2(); //undefined }; func(); //undefined
2: As an object method
const user = { userName: '小张', age: 18, selfIntroduction: function () { const str = '我的名字是:' + this.userName + ",年龄是:" + this.age; console.log(str); const loop = function () { console.log('我的名字是:' + this.userName + ",年龄是:" + this.age); }; loop(); //我的名字是:undefined,年龄是:undefined } }; user.selfIntroduction(); //我的名字是:小张,年龄是:18
const user = { userName: '小张', age: 18, selfIntroduction: function () { const str = '我的名字是:' + this.userName + ",年龄是:" + this.age; console.log(str); const that=this; const loop = function () { console.log('我的名字是:' + that.userName + ",年龄是:" + that.age); }; loop(); //我的名字是:小张,年龄是:18 } }; user.selfIntroduction(); //我的名字是:小张,年龄是:18
const user={ userName:'小张', age:18, selfIntroduction:function(){ const str='我的名字是:'+this.userName+",年龄是:"+this.age; console.log(str); } }; const other =user.selfIntroduction; other(); //我的名字是:undefined,年龄是:undefined const data={ userName:'小李', age:19, }; data.selfIntroduction=user.selfIntroduction; data.selfIntroduction(); //我的名字是:小李,年龄是:19
3: Triggered as an event in html
<body> <p id="btn">点击我</p> </body>
const btn=document.getElementById('btn'); btn.addEventListener('click',function () { console.log(this); //<p id="btn">点击我</p> })
4: new keyword (constructor)
##
const fun=function(userName){ this.userName=userName; } const user=new fun('郭德纲'); console.log(user.userName); //郭德纲
5:es6 (arrow function)
const func1=()=>{ console.log(this); }; func1(); //Window
const data={ userName:'校长', selfIntroduction:function(){ console.log(this); //Object {userName: "校长", selfIntroduction: function} const func2=()=>{ console.log(this); //Object {userName: "校长", selfIntroduction: function} } func2(); } } data.selfIntroduction();
#6: Change the point of this The three functions call, apply, and bind can artificially change the point of this of the function. I won’t go into detail about the differences between the three here. I will explain the differences between the three in detail in future blogs. Now let’s take one as an example
const func=function(){ console.log(this); }; func(); //window func.apply({userName:"郭德纲"}); //Object {userName: "郭德纲"}
To sum up a lot of this, it is the 4 points I mentioned at the beginning
1: Global variables are mounted by default in Under the window object
2: Generally this points to its caller 3: In the arrow function of es6, this points to the creator, not the caller 4: Through call, apply, Bind can change the pointer of this To be honest, it is my first time to write a blog. I am really nervous. Will anyone read my blog? Could it be written incorrectly? ...I have thought a lot better and have summarized: I welcome corrections on any shortcomings.
The above is the detailed content of Summary and detailed explanation of this pointer in javaScript. For more information, please follow other related articles on the PHP Chinese website!