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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:29:53  来源:igfitidea点击:

Access last logged value in Chrome console

javascriptgoogle-chromeconsole.log

提问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

然后我可以使用参考评估值

$_

enter image description here

在此处输入图片说明

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 的结果而不是我直接在控制台中键入的表达式时,我无法使用 $_ 访问该值。

enter image description here

在此处输入图片说明

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:

只需按照以下步骤操作:

  1. Click over the result with right button
  2. Save as global variable
  3. copy(temp1)
  1. 用右键单击结果
  2. 另存为全局变量
  3. 复制(临时1)

回答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_variablewill be logged, but you can then type global_variablein 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 $0is what you're looking for.

我想$0这就是你要找的。