简体   繁体   中英

Strict mode in Javascript

I am currently learning Javascript and came across the term "strict mode". I know that it is used to indicate to the compiler that the code should be examined stricter. Then I went on testing this: first I tried this:

 function test() { "use strict"; a = 4; alert(a); } 

I didn't get an alert and thought that it is quite logical as an error is thrown because "a" is not defined. Then I tried this:

 { "use strict"; a = 4; alert(a); } 

After refreshing the page an alert was shown saying "4". So my question is when is the strict mode "working" and when it isn't?

Quoting the MDN page on strict mode :

Strict mode applies to entire scripts or to individual functions. It doesn't apply to block statements enclosed in {} braces; attempting to apply it to such contexts does nothing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM