js中的this

严格模式下的this
this.a = 9804;
console.log('this.a === window.a===',window.a); // 9804
function a() {
  console.log(this.a);
};
a(); //   9804;     如果新增"use strict";代码前缀,由于this为undefined代码报错
xxxDom.click = () => { console.log(this) } // "use strict"下undefined; 箭头函数中原本指向window, 严格模式下函数内无法指向window除了setTimeout和setInterval中普通函数的this
构造函数的this
function constru(){
  this.a = 'constru.a';
  this.f2 = function(){
    console.log(this.b);
    return this.a;
  }
}
var o2 = new constru();
o2.b = 'o2.b';
console.log(o2.f2());   // constru.a
其它
  • 事件函数中指向事件对象
  • 对象下的函数方法指向该对象
  • 内联事件中的this一般指向事件对象,如下情况为undefined
<button onclick="alert((function(){'use strict'; return this})());">
内联事件处理
</button>

如何理解'use strict'对this的影响:
'use strict'之前定义好的对象,this是无法指向的,如window、内联事件中的事件对象在事件函数内部的严格模式....................

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言 近期对this很感兴趣,于是乎简单整理了一些关于this的技术点,加深一下对this的理解。 非箭头函数 在...
    little_short阅读 459评论 0 0
  • 初学JavaScript经常被this绕晕,所以我总结一下JavaScript中的this。首先声明本文讨论的是非...
    NSO阅读 274评论 0 1
  • 首先,明确几个基本概念 严格模式和非严格模式,即是否添加'use strict'; this的行为结果不同,本文只...
    shengbeiniao阅读 761评论 0 50
  • 1.处于window上下文或者不在任何function中时,this指向window,不管当前是否处于use st...
    kkyeer阅读 851评论 0 0
  • 1.背景介绍 什么是面向对象编程? “面向对象编程”(Object OrientedProgramming,缩写为...
    return_3711阅读 280评论 0 0