Chrome控制台清除分配和变量 [英] Chrome console clear assignment and variables

查看:2055
本文介绍了Chrome控制台清除分配和变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习JavaScript并且已经在Chrome控制台中进行了大量测试。即使我清除控制台,或使用我在其他线程中看到的任何命令(localStorage.clear()),我指定的任何变量仍会显示。

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 (localStorage.clear()) any variables I've assigned still show up.

例如,如果我执行类似 var name =Bob;

For example, if I do something like var name = "Bob";

我清除并关闭控制台,重新打开它,并查找 name ,它仍然是 Bob

I clear and close the console, reopen it, and look for the value of name, it's still Bob.

清除这些内容的最佳方法是什么?

What's the best way to clear these out?

推荐答案

在开发者控制台中声明任何变量或函数时,您所影响的是全局执行上下文,适用于网络浏览器的 window

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.

clear() 您要告诉Chrome的控制台删除这些操作的所有可见历史记录,而不是清除您附加的对象到窗口

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

如果必须通过删除重复删除多个对象的开销太多,将每个值存储为对象的属性,例如 data ,您可以在一个声明中删除所有属性:

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

这篇关于Chrome控制台清除分配和变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆