Cơ BảnJavaScript iconJavaScript

strict mode ảnh hưởng đến this như thế nào?

Trong strict mode, this ở hàm thông thường là undefined thay vì global object (window/global).

Điều này giúp phát hiện lỗi khi vô tình gọi hàm như function thay vì method.

javascript
function foo() { console.log(this); }
foo();                    // non-strict: Window (browser)
'use strict';
function bar() { console.log(this); }
bar();                    // strict: undefined

Kích hoạt bằng 'use strict'; ở đầu file hoặc hàm.

ES modules tự động bật strict mode.

Xem toàn bộ JavaScript cùng filter theo level & chủ đề con.

Mở danh sách JavaScript