chrome 的 console.log() 返回 undefined 怎么办?

2014-11-20 14:08:01 +08:00
 66beta
可能被其他JS库覆盖了,不能调试了,咋办?
11877 次点击
所在节点    问与答
21 条回复
lizheming
2014-11-20 14:10:22 +08:00
console.error
console.warn
console.table
这么多呢..别吊死在一棵树上上..
lizheming
2014-11-20 14:19:25 +08:00
如果console都被覆盖了的话也可以自己做调试输出啊..具体查看: http://segmentfault.com/blog/laopopo/1190000000731903
FrankFang128
2014-11-20 14:23:44 +08:00
本来就是返回 undefined 啊。
什么叫不能调试呢?请详述。
是在不行请用 debugger 调试
Mutoo
2014-11-20 14:32:34 +08:00
console = null; // that's evil;
learnshare
2014-11-20 14:36:23 +08:00
哪个库这么牛,直接删除 console 来退出 debug 模式...
clino
2014-11-20 14:38:58 +08:00
这不返回undefined会返回什么呢?

console.log()
undefined
Jaylee
2014-11-20 14:44:05 +08:00
@learnshare 任何一个都可以吧, console = null; 就可以

console.log的返回值就是undefined
typcn
2014-11-20 14:48:00 +08:00
写一个扩展,在页面加载完成前运行脚本。var xxx = console

然后通过chrome的api把这个xxx导入到页面里
learnshare
2014-11-20 15:04:36 +08:00
@Jaylee 主要是这个办法影响了其他使用该功能的 JS,完全是不明智的做法
livelazily
2014-11-20 19:16:43 +08:00
创建一个 iframe 对象,iframe.contentWindow.console 就是完整的每被覆盖的 console
66beta
2014-11-20 21:52:55 +08:00
@FrankFang128
是console.log('xxx');返回undefined,这样就不方便在js里面在console里显示json了~
spance
2014-11-20 22:45:52 +08:00
delete console.log;
// chrome.
console.log(XXXX);
Mutoo
2014-11-20 22:52:31 +08:00
>> console.log(JSON.stringify({a:1,b:2}, null, 2));
{
"a": 1,
"b": 2
}
<< undefined
FrankFang128
2014-11-21 01:07:34 +08:00
估计某个库写了
console = ...
那样的话就没救了。

你可以在HTML head第一行写 _console = console
idgnarn
2014-11-21 06:10:23 +08:00
debug为什么不用断点?
livelazily
2014-11-21 07:54:54 +08:00
@66beta console.log 这个函数本来就没有返回值的啊...你到底想要获取什么东西?
66beta
2014-11-21 09:22:44 +08:00
@livelazily console.log('abc'); 应该显示abc,但是没显示~只返回了undefined
66beta
2014-11-21 09:25:10 +08:00
@FrankFang128 应该就是这个情况,好恨啊,因为是个大项目,要去header里加一句着实麻烦
@idgnarn 看来要走上高大洋的debug之路了,alert和console.log该抛弃了
livelazily
2014-11-21 09:30:44 +08:00
@66beta 显示和返回值是两个东西... 如果是没有显示的话,应该是 console.log 被人覆盖了,按照我的方法就可以从别的 iframe windos 里拿一个回来; 如果是要返回值, console.log 函数是没有返回值的
66beta
2014-11-21 09:33:00 +08:00
@livelazily 谢,其实我就是想偷懒,在console被覆盖的情况下,最简单的方式可以重新用,其实就几个小json,console.log足够了~

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/147996

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX