var和let、const

64 阅读1分钟

问输出什么?

var age = 20;
const name = 'zhangsan';
const userlnfo = {
        name: 'lisi',
            age: 24,introduction: ()=>{ console.log(`My name is ${this.name} and I am ${this.age} years old `);
            }
          }
const userCard={
name: 'card',
introduction: userlnfo.introduction,
    }
const flylntroduction = userCard.introduction;
flylntroduction();

解答:var声明的变量会挂载在window上,而let和const声明的变量不会挂载到window上,它形成了一个块作用域,用this访问不到。 至于this指向,是在运行的时候的指向this,此时指向window

截屏2023-08-20 15.12.32.png