[英]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.