1.普通函數(shù)??
this指向window
? ? function fn() {?
? ? ? ? console.log(111);
? ? }
? ? fn() //或者fn.call()
2.對象方法函數(shù)? ? ?
this指向方法所屬對象
? ? let a = {
? ? ? ? sayHi: function() {
? ? ? ? ? ? console.log(222);
? ? ? ? }
? ? }
? ? a.sayHi()
3.構(gòu)造函數(shù)? ? ? ?
this指向?qū)嵗龑ο?原型對象里面的方法也指向?qū)嵗龑ο?/h3>
? ? function Star() {}
? ? new Star()
?4.綁定事件函數(shù)
?this指向綁定事件對象
? ? btn.onclick = function() {
? ? ? ? ? ? console.log(444);
? ? ? ? }
5.定時器函數(shù)
?this指向window
? ? setInterval(function() {
? ? ? ? ? ? console.log(555);
? ? ? ? }, 1000)
?6.自執(zhí)行函數(shù)
?this指向window
? ? ? ? (function() {
? ? ? ? ? ? console.log(666);
? ? ? ? })()
? ? function Star() {}
? ? new Star()
? ? btn.onclick = function() {
? ? ? ? ? ? console.log(444);
? ? ? ? }
? ? setInterval(function() {
? ? ? ? ? ? console.log(555);
? ? ? ? }, 1000)
? ? ? ? (function() {
? ? ? ? ? ? console.log(666);
? ? ? ? })()