将console.log值分配给变量 [英] Assign console.log value to a variable

查看:205
本文介绍了将console.log值分配给变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将JavaScript对象分配给使用 console.log 打印的变量?

How can I assign a JavaScript object to a variable which was printed using console.log?

我在Chrome控制台中。使用Ruby,我会使用 test = _ 来访问最新打印的项目。

I am in Chrome console. With Ruby I would use test = _ to access the most recent item printed.

推荐答案

您可以覆盖标准 console.log()功能拥有,添加你需要的行为:

You could override standard console.log() function with your own, adding the behaviour you need:

console.oldLog = console.log;

console.log = function(value)
{
    console.oldLog(value);
    window.$log = value;
};

// Usage

console.log('hello');

$log // Has 'hello' in it

这样,您不必更改现有的日志记录代码。您还可以扩展它,添加一个数组并存储打印对象/值的整个历史记录。

This way, you don't have to change your existing logging code. You could also extend it adding an array and storing the whole history of printed objects/values.

这篇关于将console.log值分配给变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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