时间:2021-07-01 10:21:17 帮助过:20人阅读
一、全局作用域或者普通函数中this指向全局对象window。
//直接打印
console.log(this) //window
//function声明函数
function bar () {conso
le.log(this)}bar() //window
//function声明函数赋给变量
var bar = function () {console.log(this)}bar() //window
//自执行函数
(function () {console.log(this)})(); //window二、方法调用中谁调用this指向谁
//对象方法调用
var person = {run: function () {console.log(this)}}person.run()// person
//事件绑定
var btn = document.querySelector("button")btn.onclick = function () {console.log(this) // btn}
//事件监听
var btn = document.querySelector("button")btn.addEventListener('click', function () {console.log(this) //btn})
//jquery的ajax
$.ajax({ self: this, type:"get", url: url, async:true, success: function (res) {console.log(this)
// this指向传入$.ajxa()中的对象
console.log(self) // window } });
//这里说明以下,将代码简写为$.ajax(obj) ,this指向obj,在obj中this指向window,因为在在success方法中,独享obj调用自己,所以this指向obj三、在构造函数或者构造函数原型对象中this指向构造函数的实例
//不使用new指向window
windowfunction Person (name) {console.log(this) // window
this.name = name;}Person('inwe')
//使用new
function Person (name) {this.name = name console.log(this) //people
self = this } var people = new Person('iwen') console.log(self === people) //true
//这里new改变了this指向,将this由window指向Person的实例对象people
new改变this指向,将this指向window改为指向person的实例people改变this的指向:
函数本身就是一个特殊类型,大多数认为是一个变量,this指向谁在函数定义时候确定不了,只有在函数执行时候才可以确定this到底指向谁,实际上this指向的是最终调用他的函数。
相关推荐:
PHP中$this的用法和访问限定符详解
修改JavaScript中的this指向的多种方法
JavaScript中的this规则及this对象用法实例
以上就是js中this的指向问题探讨的详细内容,更多请关注Gxl网其它相关文章!