js中的this

it2025-07-16  4

JavaScript this 关键词指的是它所属的对象

在方法中,this 指的是所有者对象。单独的情况下,this 指的是全局对象。在函数中,this 指的是全局对象。在函数中,严格模式下,this 是 undefined。在事件中,this 指的是接收事件的元素。

单独的 this

在单独使用时,拥有者是全局对象,因此 this 指的是全局对象。 在浏览器窗口中,全局对象是 [object Window]

var x = this; //x = object Window

事件处理程序中的 this

<button onclick="this.style.display='none'">单击来删除我!</button>

用call()和apply()调用另外函数

函数中this,指的是call()和apply()参数对象中的this

var person1 = { fullName: function() { return this.firstName + " " + this.lastName; } } var person2 = { firstName:"Bill", lastName: "Gates", } person1.fullName.call(person2); // 会返回 "Bill Gates"
最新回复(0)