# 筆記_javascript 嚴格模式 ---- ###### tags: `javascript` [鐵人賽:JavaScript 的嚴格模式 "use strict"](https://wcc723.github.io/javascript/2017/12/15/javascript-use-strict/) [Javascript 的嚴格模式 (Strict Mode):不讓你錯](https://medium.com/itsems-frontend/javascript-strict-mode-d0a3aa74458b) > 需要注意不是加在最前面就無效,並如果你在 JS 檔案中引入了很多支別的 JS,第一支 JS 的第一行加上了 ‘use strict’ 了的話,後面所有 JS 檔案都會以嚴格模式執行。 ## 'use strict' 及 this 在先前介紹 this 的時有介紹到不同的呼叫方法,在 'use strict' 的環境下的 純粹的調用 (Simple call) 的 this 不在是全域變數。 ```javascript= window.auntie = '漂亮阿姨'; function callAuntie() { 'use strict'; console.log('call:', this.auntie); } callAuntie.call({ auntie: '叫阿姨' }); // Ok callAuntie(); // 錯誤,此呼叫會導致 this 為全域 ``` 但 this 依然可作為 window 的方式傳入。 ```javascript= window.auntie = '漂亮阿姨'; function callAuntie() { 'use strict'; console.log('call:', this.auntie); } callAuntie.call(this); // Ok,call: 漂亮阿姨 ``` 現在會建議寫 JavaScript 的時候加入 'use strict',這可以改正一些編寫時的不良習慣,但也有可以因此導致專案無法運作,此時可以考慮將 'use strict' 加在函式內,避免影響過去的程式碼及相關套件。