Chrome console clear assignment and variables

前端 未结 9 1432
野的像风
野的像风 2020-12-02 13:56

I am learning JavaScript and have been doing a lot of testing in the Chrome console. Even if I clear the console, or use any of the commands I\'ve seen in other threads (

9条回答
  •  抹茶落季
    2020-12-02 14:38

    A simple solution to this problem is to wrap any code that you don't want in the global scope in an immediately-invoked function expression (IIFE). All the variables assigned in the function's scope are deallocated when the function ends:

    (function() {
    
        // Put your code here...
    
    })();
    

    For more info on IIFEs: https://en.wikipedia.org/wiki/Immediately-invoked_function_expression

    [Update]

    In ES6 you can use blocks (so long as you use let instead of var):

    {
    
        // Put your code here...
    
    }
    

    For more info on blocks: http://exploringjs.com/es6/ch_core-features.html#sec_from-iifes-to-blocks

提交回复
热议问题