简体   繁体   中英

How is console.log() implemented?

Where can I learn about the implementations of 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. Does console.log just echo input back into a part of the screen? Does it save logged strings to disk when parsing js and then write it to back to the screen? 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").

For this question specifically, the behaviour as of 23 March 2021 is prescribed as:

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).

(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)

console.log() can be used to debug variables, test if functions are being called, things along those lines, here's an example

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

console: 1 2 3 4 5 6 7 8 9

1 more example:

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

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