简体   繁体   English

为什么console.log不能在JSC环境中工作,但它可以在Safari的调试控制台中运行

[英]Why doesn't console.log work in the JSC environment but it works in Safari's debug console

When I use the JSC (JavaScriptCore) engine provided in the System Library, it acts differently then when using Safari's debug console 当我使用系统库中提供的JSC(JavaScriptCore)引擎时,它的行为与使用Safari的调试控制台时不同

$ /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc
>>> console.log("hello");
Exception: TypeError: undefined is not an object (evaluating 'console.log')

When console.log("hello"); console.log("hello"); works perfectly fine in Safari. 在Safari中完美运行。

TL;DR TL; DR

var Console = function () {
    this.log = function(msg){ debug(msg) }; 
};
var console = new Console();
console.log("hello");

Safari creates a console object that is available in the debug console, but not in the JSC environment. Safari创建一个控制台对象,该对象在调试控制台中可用,但在JSC环境中不可用。 See Safari's console documentation here 在此处查看Safari的控制台文档

Adding my own console object that wraps the JSC debug method solved my problem: 添加我自己的包装JSC调试方法的控制台对象解决了我的问题:

$ /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc
>>> var Console = function () {
...     this.log = function(msg){ debug(msg) };
... };
undefined
>>> var console = new Console();
undefined
>>> console.log("hello");
-> hello
undefined

JSC中不存在控制台对象 - 如果您喜欢JavaScriptCore console.log,可以添加它

我最终得到了这个可以在其他JS引擎上工作的衬里以及JSC兼容性:

console = console || { log: (...args) => debug(Array.prototype.slice.call(args).join(' ')) }

I learned from the answers above. 我从上面的答案中学到了很多。

Though, as I not see a goal, I presume it was to see output on stdout: 虽然,因为我没有看到目标,我认为是在stdout上看到输出:

print('string')

And when ready, use a stream editor to substitute 'print' with 'console.log'. 准备好后,使用流编辑器将'print'替换为'console.log'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 console.log() 在 Safari 6.0 Web Inspector 中不起作用 - console.log() doesn't work in Safari 6.0 Web Inspector 返回不起作用,console.log有效 - return doesn't work, console.log works 为什么console.log不能作为参数? - Why console.log doesn't work as an argument? 为什么“console.log”在“return”命令后不起作用? - Why "console.log" doesn't work after a "return" command? 为什么“console.log()”在这个网站上不起作用? - Why doesn't "console.log()" work on this website? console.log有时不起作用 - console.log doesn't work sometimes 在Safari Devtools中打开了控制台,但是console.log('hello')无法正常工作 - Console open in Safari Devtools, but console.log('hello') doesn't work 为什么console.log(窗口)工作但JSON.stringify(窗口)不起作用,我怎么能打败它? - Why console.log(window) works but JSON.stringify(window) doesn't work, and how can I beat that? 当console.log作为参数传递时,它起作用,但是当array.push被传递参数时,它不起作用,为什么? - When console.log passed as a parameter it works, but when array.push is passed a parameter it doesn't work why? jQuery .on()问题,console.log()有效,但元素交互不起作用 - jQuery .on() issue, console.log() works but element interaction doesn't
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM