简体   繁体   English

Chrome 控制台清除分配和变量

[英]Chrome console clear assignment and variables

I am learning JavaScript and have been doing a lot of testing in the Chrome console.我正在学习 JavaScript,并且一直在 Chrome 控制台中进行大量测试。 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.即使我清除控制台,或使用我在其他线程 ( localStorage.clear() ) 中看到的任何命令,我分配的任何变量仍会显示。

For example, if I do something like var name = "Bob";例如,如果我做类似var name = "Bob";

在此处输入图像描述

I clear and close the console, reopen it, and look for the value of name , it's still Bob .我清除并关闭控制台,重新打开它,然后查找name的值,它仍然是Bob

在此处输入图像描述

What's the best way to clear these out?清除这些的最好方法是什么?

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).解决这个问题的一个简单方法是在一个立即调用的函数表达式 (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有关 IIFE 的更多信息: 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 ):在 ES6 中,您可以使用块(只要您使用let而不是var ):

{

    // Put your code here...

}

For more info on blocks: http://exploringjs.com/es6/ch_core-features.html#sec_from-iifes-to-blocks有关块的更多信息:http: //exploringjs.com/es6/ch_core-features.html#sec_from-iifes-to-blocks

Easiest way to clear data from the console is to refresh the page .从控制台清除数据的最简单方法是刷新页面

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 .在开发人员控制台中声明任何变量或函数时,您所影响的是全局执行上下文,对于 Web 浏览器来说是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 .当您clear()控制台时,您是在告诉 Chrome 删除这些操作的所有可见历史记录,而不是清除您附加到window的对象。

Clearing the console doesn't clear out the variables from the memory rather it just clears the data from the user interface.清除控制台不会清除内存中的变量,而只是清除用户界面中的数据。

Reloading the page clears the console data.重新加载页面会清除控制台数据。 I hope that should be fine as you mentioned that you are just testing and learning javascript.我希望这应该没问题,因为您提到您只是在测试和学习 javascript。

Hope that helps!希望有帮助!

Just Reload for new context with clear history and old commands execution只需重新加载具有清晰历史记录和旧命令执行的新上下文

Things have changed a lot on Chrome dev tools. Chrome 开发工具发生了很大变化。 delete name does not help; delete name没有帮助;

//for example if you have declared a variable 
const x = 2;
//Now sometime later you want to use the same variable 
const x = 5; // you will get an error 
//if you try to delete it you will get false
delete x;

But a reload of page while console is still open.但是在控制台仍然打开时重新加载页面。 will refresh the console context and now the x is not available and you can redefine it.将刷新控制台上下文,现在x不可用,您可以重新定义它。

I think the best way to clear the defined varible would be:我认为清除已定义变量的最佳方法是:

window.location.reload(); window.location.reload();

It would automatically remove all the declared variable at once.它会立即自动删除所有声明的变量。

If you right-click the refresh icon in chrome(chrome devtools need to be opened, F12 / Right Click on the page -> Inspect ), after a second or two you will get the option to hard reload.如果您右键单击 chrome 中的刷新图标(需要打开 chrome devtools, F12 /右键单击​​页面 -> Inspect ),一两秒后您将获得硬重新加载的选项。 Hard Reload will clear all stored variables. Hard Reload 将清除所有存储的变量。

Ctrl + Shift + R (Windows) / Cmd + Shift + R (Mac) will do the same without opening the devtools. Ctrl + Shift + R (Windows) / Cmd + Shift + R (Mac) 将在不打开 devtools 的情况下执行相同的操作。

如果要删除该变量,则

delete name;

实际上有一个答案,不确定这是新的还是已经存在了一段时间,但是如果您执行硬刷新,即 control + shift + r,那么控制台值将被完全删除,您可以重新实例化相同的变量和功能等。

name is already defined. name已定义。

在此处输入图像描述

So - it's already in the global space.所以 - 它已经在全球空间中。

If you do the same thing with a variable like hello - and then refresh, it will clear the memory.如果你对像hello这样的变量做同样的事情 - 然后刷新,它会清除内存。

Because name is already defined, it will remain.因为name已经定义,所以它会保留。 Refreshing - will not work.令人耳目一新 - 将不起作用。

在此处输入图像描述

在此处输入图像描述

(this was really confusing me - but would not have - if I had chosen almost any other variable for my class demonstration!) (这真的让我感到困惑 - 但不会 - 如果我为课堂演示选择了几乎任何其他变量!)

Up until a few days ago CTRL + Shift + R worked for me.直到几天前CTRL + Shift + R为我工作。

However they have made it very easy to clear console.但是,它们使清除控制台变得非常容易。

Open chrome dev tools and right-click on an empty space.打开 chrome 开发工具并右键单击空白区域。 If you right-click on the line with the cursor, it won't show you the right options.如果您用光标右键单击该行,它不会显示正确的选项。 The right options are clear console and clear console history.正确的选项是清除控制台和清除控制台历史记录。

Clearing console history will clear all variables and their data stored in the console.清除控制台历史记录将清除控制台中存储的所有变量及其数据。 Hope this helps.希望这可以帮助。 It was driving me nuts too and there was no clear answer.这也让我发疯了,没有明确的答案。

似乎可以通过鼠标右键上下文菜单清除控制台历史记录。

Console.clear()

在您的浏览器控制台中使用此命令,看看它的魔力,使用起来非常方便,并且按照使用 chrome 控制台的最佳实践完美运行,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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