Javascript Chrome 控制台清除赋值和变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34270829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Chrome console clear assignment and variables
提问by spex5
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.
我正在学习 JavaScript,并在 Chrome 控制台中进行了大量测试。即使我清除了控制台,或者使用我在其他线程 ( 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?
清除这些的最佳方法是什么?
采纳答案by sdgluck
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 visiblehistory of these operations, not clear the objects that you have attached to window
.
当您clear()
使用控制台时,您告诉 Chrome 删除这些操作的所有可见历史记录,而不是清除您附加到的对象window
。
To do this you must manually delete
each object by its reference:
为此,您必须delete
通过引用手动每个对象:
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:
如果必须通过重复删除多个对象的开销delete
太大,请将每个值存储为对象的属性,例如data
,您可以在一个语句中与所有属性一起删除:
var data = {};
data.name = 'Bob';
data.age = 60;
delete data;
data.name //=> ReferenceError: data is not defined
data.age //=> ReferenceError: data is not defined
回答by pjivers
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:
此问题的一个简单解决方案是将您不需要的任何代码包装在立即调用的函数表达式 (IIFE) 中的全局范围内。当函数结束时,在函数作用域中分配的所有变量都会被释放:
(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
回答by hkasera
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!
希望有帮助!
回答by Tarandeep Singh
Just Reload for new context with clear history and old commands execution
只需重新加载具有清晰历史记录和旧命令执行的新上下文
Things have changed a lot on chrome dev tools. delete name
does not help;
chrome 开发工具的情况发生了很大变化。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. Hope this helps someone testing or trying things on the console of chrome. i use it a lot.
但是在控制台仍然打开时重新加载页面。将刷新控制台上下文,现在 x 不可用,您可以重新定义它。希望这有助于有人在 chrome 控制台上测试或尝试一些东西。我经常使用它。
回答by Pawan lakhera
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.
它会立即自动删除所有声明的变量。
回答by user2879041
If you want to remove that variable then
如果你想删除那个变量然后
delete name;
回答by user2841183
Console.clear()
Use this command in your browser console and see the magic, very handy to use and works perfectly as per best practices of using chrome console,
在您的浏览器控制台中使用此命令并查看其神奇之处,使用起来非常方便,并且可以按照使用 chrome 控制台的最佳实践完美运行,