①普通的函數調用是經過給函數名或者變量名添加()的方式執行。函數
function fn(){ console.log(1); }; fn();
②構造函數,經過new關鍵字進行調用(也能夠使用()調用,只是功能不全)this
function Student(name){ this.name = name; }; var s1 = new Student("li");
③對象中的方法,經過對象打點調用函數,而後加括號();
內部的 this 默認指向的是調用的對象本身code
var Student = { name:"lu", message: function(){ console.log(this.name + " is a student"); } } Student.message();
④事件函數,不須要加特殊符號,只要事件被觸發,會自動執行函數;
內部的 this 默認指向的是事件源對象
document.onclick = function(){ console.log("hello"); }
⑤定時器、延時器中的函數,不須要加特殊符號,只要執行後,在規定的時間自動執行;
內部的 this 默認指向是window事件
setInterval(function(){ console.log(1); },1000);
this的指向是須要聯繫執行上下文,在調用的時候,是按照什麼方式調用,指向是不同的原型
調用方式 | 非嚴格模式 | 備註 |
---|---|---|
普通函數調用 | window | 嚴格模式下是 undefined |
構造函數調用 | 實例對象 | 原型方法中 this 也是實例對象 |
對象方法調用 | 該方法所屬對象 | 緊挨着的對象 |
定時器函數 | window | |
事件綁定方法 | 綁定事件對象 |