简体   繁体   中英

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

$ /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"); works perfectly fine in Safari.

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. See Safari's console documentation here

Adding my own console object that wraps the JSC debug method solved my problem:

$ /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:

print('string')

And when ready, use a stream editor to substitute 'print' with 'console.log'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM