惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Troy Hunt's Blog
P
Proofpoint News Feed
V
Vulnerabilities – Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
K
Kaspersky official blog
Cyberwarzone
Cyberwarzone
T
Tor Project blog
Cisco Talos Blog
Cisco Talos Blog
S
Securelist
L
Lohrmann on Cybersecurity
Security Latest
Security Latest
T
Threatpost
H
Heimdal Security Blog
W
WeLiveSecurity
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
G
GRAHAM CLULEY
IT之家
IT之家
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
TaoSecurity Blog
TaoSecurity Blog
A
About on SuperTechFans
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Last Week in AI
Last Week in AI
T
The Blog of Author Tim Ferriss
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Microsoft Azure Blog
Microsoft Azure Blog
Hugging Face - Blog
Hugging Face - Blog
Google DeepMind News
Google DeepMind News
量子位
Stack Overflow Blog
Stack Overflow Blog
Know Your Adversary
Know Your Adversary
B
Blog RSS Feed
阮一峰的网络日志
阮一峰的网络日志
WordPress大学
WordPress大学
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
AI
AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
GbyAI
GbyAI
Vercel News
Vercel News
C
Cyber Attacks, Cyber Crime and Cyber Security
Latest news
Latest news
D
Darknet – Hacking Tools, Hacker News & Cyber Security
大猫的无限游戏
大猫的无限游戏
Forbes - Security
Forbes - Security

博客园 - Albert_MIN

在 ASP.NET Core MVC 中,接收数据的几种方式 Content-Type 对应不同的前端数据结构与构造方式 Revit二次开发 钢筋生成API(二) Revit二次开发 钢筋生成API(一) revit二次开发 钢筋布置方式 revit二次开发之 钢筋功能详细分析 revit 二次开发之收集器、过滤器和选择器 Revit Server的注意要配置说明 WCF文件配置服务 WCF服务的各种绑定 Revit二次开发之 对象的隐藏与显示 Revit二次开发之 GeometryObject分析 Revit二次开发之 Material 分析 Revit二次开发之 PolymeshTopology Revit 二次开发之 图纸的导出 Revit开发之 IExportContext接口详细 JavaScript 困惑之 ArrayBuffer Revit二次开发之 族的创建 Revit二次开发 钢筋生成 Revit二次开发之 尺寸标线(二) Revit二次开发之 尺寸标线
JS 困惑之this的指向
Albert_MIN · 2022-10-12 · via 博客园 - Albert_MIN

JS中,this到底指向谁,一直是比较困惑的问题,由于this的指向问题造成bug,在程序中经常出现,如何正确理解this的应用,是写好js代码的关键。

案例1:

function thiswindow() {
     console.log(this === window);   //输出为true
}
thiswindow();

我们运行以上代码,发现this指向的是window对象。我们把代码稍作改变如下:

案例2

function thiswindow() {
     console.log(this === window);     //输出为true
     console.log(this);      //输出为thiswindow对象
}
var s = new thiswindow();

以上案例说明,this的指向会随着调用方式的不同发生变化,变化的原则就是,this指向函数的调用这,在第一事例中,函数的调用者是window,第二个实例中,由于使用new创建了一个对象,则函数的调用时这个对象触发,则函数的调用者变成了thiswindow对象本身。

案例3

function thiswindow(msg) {

    this.name = msg;
}
thiswindow.prototype.show = function () {
     console.log(this.name);
}

thiswindow("m1")
console.log(this.name);  //输出m1
var s1 = new thiswindow("s1"); 
var s2 = new thiswindow("s2"); 
s1.show();  //输出s1
s2.show();  //输出s2
s1.show();  //输出s1
console.log(this.name);  //输出m1

通过以上案例,就可以明确知道,调用方式的不同,造成this的指向时不同的,对于thiswindow函数直接调用,this指向的是window,所以两次的直接输出this.name都是m1,对于声明的s1\s2对象来说,器指向的就声明的对象本身,则this指向的是对象本身,并且就算s2改变了name,也不会影响s1对象中的name信息。

对事件和setTimeout等对象,其this指向

案例4

  setTimeout(function() {
    console.log(this === window);   //输出为true
}

案例5

  var btn = document.querySelector("button")
        btn.onclick = function(e) {
        console.log(this);//this的指向是[object HTMLButtonElement]
        console.log(e.target)
}

这个里面this和target有点迷惑性,为什么有了this,还要定义target呢,其实2两者的作用是不一样的,this指向的事件绑定对象,target指定的是事件触发对象。

例子如下:

<div id="ddd">
        <button id="bbb">点击</button>
    </div>
const b = document.querySelector("div");
b.addEventListener("click", function (e) {

    console.log(this);   //输出div
     console.log(e.target); 输出button对象
})