You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Chrome代码片段中'use strict'执行不一致问题求助

Troubleshooting Inconsistent 'use strict' Behavior in Chrome DevTools Snippets

Hey there! Let's dig into this quirky issue you're facing with 'use strict' in Chrome DevTools Snippets—this kind of inconsistent behavior almost always ties back to leftover state in your browser's runtime context.

What's Probably Going On

The core problem here is global variable pollution. At some point before your current test, the variable thVal was accidentally added to the global window object (maybe from a previous run where you forgot to comment out a declaration, a typo in another snippet, or even a stray console command).

Strict mode only throws a ReferenceError when you assign to an undeclared variable. If thVal already exists in the global scope, your code is just updating that existing variable instead of violating strict mode rules. Other names like thValue or theVol work as expected because they haven't been accidentally added to the global scope yet.

Fixes to Try

  • Verify if thVal exists globally
    Open the Console tab in DevTools, type thVal, and hit Enter. If it returns a value (like 1 from your test), that confirms it's lingering in the global scope.

  • Reset the runtime context
    Simply refresh your browser window. This clears all leftover global variables from previous script runs. After refreshing, re-run your snippet—you should see the Uncaught ReferenceError: thVal is not defined error as expected.

  • Manually delete the global variable
    If you don't want to refresh, run this command in the Console:

    delete window.thVal
    

    Then re-execute your code snippet. The strict mode error should now trigger correctly.

  • Double-check snippet save status
    Make sure you're clicking the save icon (disk symbol) in the Sources > Snippets panel after editing your code. Sometimes DevTools doesn't auto-save changes, so you might be running an older version of the snippet where thVal was declared.

Why This Works

Strict mode enforces that variables must be declared with var, let, or const before assignment. If a variable with the same name already exists globally (from past code runs), the assignment doesn't violate this rule—it just modifies the existing global variable. Refreshing or deleting the global variable removes that reference, so the next assignment triggers the expected error.

内容的提问来源于stack exchange,提问作者longoria

火山引擎 最新活动