0

I just realize that I can use the function console.log to read a codebase of any javascript function. Anyone how it happens? and what behind the sense of this action (read codebase) Example: Open chrome console and paste this code

function superMan(){
    var i=i+1;
    var j=i+1;
}

then you use the function console.log like console.log(superMan) and you can see the codebase of SuperMan enter image description here

TrungNgo
  • 26
  • 3
  • the function is just a string that has been parsed by the javascript engine ... the string hasn't disappeared, so, it's able to be output – Bravo May 04 '22 at 03:48

2 Answers2

0

In javascript, When a program is run a execution context is created. There are 2 phases in which its created namely below.

  1. Creation phase
  2. Execution phase

What you are referring to in the question is mostly related to the creation phase. In this phase all the variables are declared and initialized to undefined ( incase of var) and for variables with let and const they are declared in the Temporal Dead Zone and are unreachable and only reachable when the code is executed in execution context.

For functions(specifically Function statements and not function expressions) these are taken in as it is in memory and stored. So, when you try to log in the function in console.log it shows the variable in memory. This will be also true for function expression once the execution runs over it(since before its treated like a normal variable and not function). They will be showed as it will now be pointing to the function directly assigned to that variable. Also, You need to understand function in Javascript are first-class functions meaning it can be passed and treated like a normal variable (containing a number) and so.

innocent
  • 864
  • 6
  • 17
0

It's part of the spec but a good reason to use it is in edge-cases where the code of the function could be important to its execution (usually for an API, interface or some form of abstraction) where you need to know a bit more about the function before acting upon it. There are many use-cases, but it can be particularly useful to try to detect and handle edge-cases for a transpiler (the code you write isn't verbatim to the code that the browser sees)

tresf
  • 7,103
  • 6
  • 40
  • 101