简体   繁体   English

console.log() 是如何实现的?

[英]How is console.log() implemented?

Where can I learn about the implementations of console.log?我在哪里可以了解 console.log 的实现? To be clear, I know HOW to use console.log(), but I want to see the underlying implementations of console.log for some of the major browsers.需要明确的是,我知道如何使用 console.log(),但我想查看一些主要浏览器的 console.log 的底层实现。 Does console.log just echo input back into a part of the screen? console.log 是否只是将输入回显到屏幕的一部分? Does it save logged strings to disk when parsing js and then write it to back to the screen?解析js时是否将记录的字符串保存到磁盘然后将其写回屏幕? etc.等等

For those still finding this question many years later, all Console behaviour (including the algorithms that must be implemented) are defined in https://console.spec.whatwg.org (and if it's not in there, officially, its implementation is undefined and literally "whatever works").对于多年后仍然发现这个问题的人,所有控制台行为(包括必须实现的算法)都在https://console.spec.whatwg.org中定义(如果它不在那里,正式地,它的实现是未定义的字面意思是“任何有效的”)。

For this question specifically, the behaviour as of 23 March 2021 is prescribed as:具体而言,对于这个问题,截至 2021 年 3 月 23 日的行为规定为:

If the console is not open when the printer operation is called, implementations should buffer messages to show them in the future up to an implementation-chosen limit (typically on the order of at least 100).如果调用打印机操作时控制台未打开,则实现应缓冲消息以在将来显示它们,直至实现选择的限制(通常至少为 100 条)。

(Although of course IE is no longer nearly as relevant in 2021 as it was back in 2014, with the very last version of IE finally entering EOL on June 15, 2022) (当然 IE 在 2021 年不再像 2014 年那样重要,IE 的最后一个版本终于在 2022 年 6 月 15 日进入 EOL)

console.log() can be used to debug variables, test if functions are being called, things along those lines, here's an example console.log() 可用于调试变量,测试是否正在调用函数,类似的事情,这里有一个例子

for(i=0;i<10;i++){
  console.log(i)
}

console: 1 2 3 4 5 6 7 8 9控制台:1 2 3 4 5 6 7 8 9

1 more example:还有1个例子:

function start(){
  console.log("starting...")
  started = true
  console.log(started)
}

console: "starting..." true控制台:“开始...”真

I don't think this is part of any specification. 我不认为这是任何规范的一部分。 You can read that and the related links : https://developer.mozilla.org/en-US/docs/Web/API/console.log 您可以阅读该相关链接: https//developer.mozilla.org/en-US/docs/Web/API/console.log

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

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