Chrome console clear assignment and variables

前端 未结 9 1427
野的像风
野的像风 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:42

    What you are affecting when declaring any variables or functions within the developer console is the global execution context, which for web browsers is window.

    When you clear() the console you are telling Chrome to remove all visible history of these operations, not clear the objects that you have attached to window.

    To do this you must manually delete each object by its reference:

    delete name;
    name //=> undefined
    

    If the overhead of having to repeatedly remove multiple objects via delete is too much, store each value as a property of an object, e.g. data, that you can delete along with all properties in one statement:

    var data = {};
    data.name = 'Bob';
    data.age = 60;
    delete data;
    data.name //=> ReferenceError: data is not defined
    data.age //=> ReferenceError: data is not defined
    

提交回复
热议问题