【复习】javascript this的指向

    技术2022-07-10  199

    this的指向,是当我们调用函数的时候确定的,调用的方式不同决定this的指向不同。this一般指向调用者

    调用方式this指向示例普通函数调用window function fn() { console.log('这是一个函数要执行的代码'); } fn();

     

    构造函数调用实例对象,原型对象中的方法也指向实例对象 function Person (uname, age) { // 属性 this.uname = uname; this.age = age; // 方法 // this.chi = function () { // console.log('吃饭'); // } }

     

    对象方法调用该方法所属对象 var obj = { taiji : function () { console.log(this); } } obj.taiji();

     

    事件绑定方法绑定事件对象 var btn = document.getElementById('btn'); btn.onclick = function () { console.log(this); }

     

    定时器函数window window.setInterval(function () { console.log(this); }, 1000);

     

    立即执行函数window (function () { console.log(this); }())

     

     

    Processed: 0.010, SQL: 12