javascript 在 Chrome 控制台中访问最后记录的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15895579/
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
Access last logged value in Chrome console
提问by Robin Drexler
When I evaluate an expression directly in the Chrome Console like
当我直接在 Chrome 控制台中评估表达式时
1 + 1
then I can reference to the evaluated value using
然后我可以使用参考评估值
$_
However, I can't access the value with $_, when the value is a result of a console.log, coming from inside of my application, instead of an expression I typed directly into the console.
但是,当值是来自应用程序内部的 console.log 的结果而不是我直接在控制台中键入的表达式时,我无法使用 $_ 访问该值。
Is there a way to access the last evaluated expression, regardless where it came from?
有没有办法访问最后一个评估的表达式,不管它来自哪里?
回答by sdr
After it's been logged to the console, you can right click on it and get an option to Store as global function. Clicking this will define a new variable like 'temp1' which will point to the variable. Here's a video of it in action (not mine).
将其登录到控制台后,您可以右键单击它并获得“存储为全局函数”的选项。单击此按钮将定义一个新变量,如“temp1”,它将指向该变量。这是它的运行视频(不是我的)。
回答by Alessander Fran?a
Just follow these steps:
只需按照以下步骤操作:
- Click over the result with right button
- Save as global variable
- copy(temp1)
- 用右键单击结果
- 另存为全局变量
- 复制(临时1)
回答by laktak
You can only copy & paste.
您只能复制和粘贴。
See all available commands and shortcuts:
查看所有可用的命令和快捷方式:
https://developers.google.com/chrome-developer-tools/docs/commandline-apihttps://developers.google.com/chrome-developer-tools/docs/shortcuts
https://developers.google.com/chrome-developer-tools/docs/commandline-api https://developers.google.com/chrome-developer-tools/docs/shortcuts
回答by sealocal
A work-around for this is to define a variable in the global namespace. Presumably, your console.log(local_variable)
is inside a function.
对此的解决方法是在全局命名空间中定义一个变量。据推测,您console.log(local_variable)
在一个函数内。
<script>
var global_variable = null;
function some_function() {
var local_variable = 0;
global_variable = local_variable;
console.log(local_variable);
}
</script>
Here, when some_function()
is called, your local_variable
will be logged, but you can then type global_variable
in your console to get its value quickly and work with it.
在这里,当some_function()
被调用时,您local_variable
将被记录,但您可以global_variable
在控制台中输入以快速获取其值并使用它。
回答by sealocal
You couldaccess any evaluated expression at any point in execution with Chrome's DevTools by setting breakpoints.
通过设置断点,您可以在 Chrome 的 DevTools 的任何执行点访问任何评估表达式。
Your logged expression should have a clickable line number in the console - follow the link, then set a breakpoint on the line of code (which should be your console.log
).
您记录的表达式应该在控制台中有一个可点击的行号 - 按照链接,然后在代码行(应该是您的console.log
)上设置一个断点。
Full guide on breakpoints:
https://developers.google.com/web/tools/chrome-devtools/javascript/breakpoints
断点完整指南:https:
//developers.google.com/web/tools/chrome-devtools/javascript/breakpoints
回答by Romainpetit
I think $0
is what you're looking for.
我想$0
这就是你要找的。