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

推荐订阅源

The Hacker News
The Hacker News
B
Blog RSS Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
宝玉的分享
宝玉的分享
L
LangChain Blog
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
F
Full Disclosure
T
The Blog of Author Tim Ferriss
月光博客
月光博客
有赞技术团队
有赞技术团队
Last Week in AI
Last Week in AI
aimingoo的专栏
aimingoo的专栏
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Stack Overflow Blog
Stack Overflow Blog
WordPress大学
WordPress大学
MongoDB | Blog
MongoDB | Blog
小众软件
小众软件
博客园 - Franky
B
Blog
博客园_首页
C
CERT Recently Published Vulnerability Notes
Hugging Face - Blog
Hugging Face - Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
K
Kaspersky official blog
I
Intezer
P
Proofpoint News Feed
博客园 - 叶小钗
S
Schneier on Security
P
Privacy & Cybersecurity Law Blog
Recorded Future
Recorded Future
NISL@THU
NISL@THU
A
Arctic Wolf
Know Your Adversary
Know Your Adversary
C
Cyber Attacks, Cyber Crime and Cyber Security
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Register - Security
The Register - Security
C
Cisco Blogs
大猫的无限游戏
大猫的无限游戏
S
Secure Thoughts
T
The Exploit Database - CXSecurity.com
Forbes - Security
Forbes - Security
Project Zero
Project Zero
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Cisco Talos Blog
Cisco Talos Blog
D
Docker
Blog — PlanetScale
Blog — PlanetScale
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
人人都是产品经理
人人都是产品经理

博客园 - howhy

html 元素包含关系 空对象 Object.keys for ...in Reflect.ownKeys ...区别 逻辑运算符和空值运算符 运算符优先级 js 类型显式转换 js 高级函数 js 方法重载 fetch timeout js 任务顺序执行 暂停 js 并发任务 判断两个对象是否相同 js 动态拦截属性 js 通用动画 js groupby js 防抖和节流 实现 instanceof 操作符 js 单例模式 js 数组去重和扁平方法 js 继承方法 js new的过程实现 js deepCopy js '=='的隐性类型转换规则
this 指向
howhy · 2026-04-24 · via 博客园 - howhy
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css">
    <style>
    </style>
</head>
<body>
    <div class="box">
        <button id="btn">click</button><br> 
        <button onclick="console.log(this)">clickthis</button><br> <!--  this指向button自身 -->
        <button onclick="(function(){console.log(this)})()">clickwindow</button><!--  this指向window -->
    </div>
    <script>
     
        // "use strict"
        function sayHi(){
            console.log(1111,this)
        }
        //sayHi()//独立函数 严格模式undefined 非严格模式window
        let user = {
            uname: 'John',
            sayHi()
            {
                console.log(this.uname)
            },
            sayHi2: ()=>
            {
                console.log(this.uname)
            },
            sayHi3(){
                function innerSayHi(){
                    console.log('inner',this.uname)
                }
                const innerSayHi2=()=>{
                    console.log('inner2',this.uname)
                }
                setTimeout(innerSayHi2,1000)
                setTimeout(()=>{
                    console.log('setTimeout2',this.uname)//this user john
                },1000)
                innerSayHi();//this window undefined
                //innerSayHi2();//this user john
            }
        }
        var aa=user.sayHi
        window.aa()//const aa=user.sayHi 报错  var aa=user.sayHi undefined 
        user.sayHi2() //箭头函数的this指向window undefined
        user.sayHi3();
        const btn=document.getElementById('btn');
        btn.onclick=function(){
            console.log(this,this.id) //btn
        }
        // btn.onclick=()=>{
        //     console.log(this,this.id) //window
        // }
        //call apply bind 显式改变this指向 new 隐性改变this指向  setTimeout 内普通函数 this指向window 箭头函数 this指向外部this
     //
普通函数this不是定义时确定的,是调用时确定的 箭头函数this需定义时确认的 继承外部作用域this 2、普通函数可以改变this 箭头函数不可以 3、普通函数有自己的this取决于调用方式 箭头函数没有自己this取决外部作用域this

        const students={
            names:['john','tom','lucy'],
            className:'三年级',
            showName(){
                // this.names.forEach(function(name){
                //     console.log(this.className,name)
                // },this)
                this.names.forEach((name)=>{
                    console.log(this.className,name)
                })
            }
        }
        students.showName()



</script> </body> </html>