在对象方法中,this 指的是此方法的“拥有者”。this 指的是 person 对象。person 对象是 fullName 方法的拥有者。
var person = { firstName: "Bill", lastName : "Gates", id : 678, fullName : function() { return this.firstName + " " + this.lastName; } };在单独使用时,拥有者是全局对象,因此 this 指的是全局对象。在浏览器窗口中,全局对象是 [object Window]:
var x = this;在 JavaScript 函数中,函数的拥有者默认绑定 this。因此,在函数中,this 指的是全局对象 [object Window]。
<button οnclick="test()">click</button>
<script> var a = 1; var b = 2; function test(){ let c = this.a + this.b; console.log(c);//3 } </script>
在 HTML 事件处理程序中,this 指的是接收此事件的 HTML 元素:
<button οnclick="this.style.display='none'"> 点击来删除我! </button>