当前位置:首页 > Web开发 > 正文

function obj(){this.attr1 = 1;}var object = new obj();objec

2024-03-31 Web开发

1.普通函数挪用时:this指向window。

function f(){ console.log(this); } f();

2.要领挪用时:this指向挪用此要领的东西。

var obj = { fn:function(){ console.log(this); } } obj.fn();

3.作为结构函数挪用时:this指向此结构函数创建的东西。

function obj(){ this.attr1 = 1; } var object = new obj(); object();

4.作为事件绑按时的措置惩罚惩罚函数:this指向绑定该事件的东西。

btn.onclick = function(){ console.log(this); }

5.作为按时器中的措置惩罚惩罚函数:this指向window。

setInterval(function(){ console.log(this); },200);

终极总结:函数中this的指向在没有进行挪用前是不明确的,,只有当函数挪用时this才华确定指向,谁挪用的函数,this就指向谁!

关于JS函数中this指向问题的总结

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/30204.html