0

function callDad () {
  var numb = '0122822122';
  console.log('Calling ' + numb);
}

callDad();

1) Since the function callDad is declared globally it is saved in the global execution context before being invoked.
2) Once the function callDad is invoked a new execution context is created and executed on the stack.

Question: What happens to the original reference to the function in the global execution context? Is the function stored twice in memory?

Additional Q/A (For Reference):

Q: @Bergi Thanks for the reply. I think the question should be: Since the global execution context already stores the function in memory. Why does the new execution context have a new creation phase? Thanks

A: @Ash The creation phase is for the variables inside the called function body, specifically numb in your example

Q: @Rodrigo So once the function is called, it is referenced from the window object? Since reference in memory and a creation phase already exists for function declarations, then why do they say that every new execution context has a new creation phase?

A: Yes, the function is always referenced from window object. The execution context is just the scope and the scope chain (variables from other scopes in closures). Once the execution context is created, the creation phase allocates the memory for the variables inside that context. Oh, and remember that, in JS, you have function scope, not block scope.

Ash
  • 1
  • 1
  • What reference to a function? Function are hoisted on top of the scope, they are not referenced twice, no. – Roberto Zvjerković Nov 20 '18 at 12:54
  • "*a new execution context is created*" - this does not create another function. It doesn't create a new variable for the function either (it only creates the `numb` variable) – Bergi Nov 20 '18 at 13:01
  • I didn't ask if it created another function. I asked about the memory allocated to the function. – Ash Nov 20 '18 at 13:06
  • The allocated memory for the function doesn't change, why do you think it should? *Nothing* happens to the original reference, it stays in the scope where it is located. – Bergi Nov 20 '18 at 13:08

2 Answers2

2

What happens to the original reference to the function in the global execution context?

Nothing. It gets stored usually only once.

(There might be some exceptions when a parsed & optimised function is stored multiple times for different argument types, but that's implementation specific and beyond this question)


Is the function stored twice in memory?

No. But a function's execution context is stored (and disposed of) multiple times - as often as you call the function.

Coloured Panda
  • 3,293
  • 3
  • 20
  • 30
  • Thanks for the reply. I think the question should be: Since the **global** execution context already stores the function in memory. Why does the new execution context have a **new creation phase**? Thanks – Ash Nov 20 '18 at 13:17
  • 1
    @Ash The creation phase is for the variables *inside* the called function body, specifically `numb` in your example – Bergi Nov 20 '18 at 13:19
  • Thank you, much appreciated – Ash Nov 20 '18 at 13:29
0

In the browser environment (but not in Node.js), the function will be attached to the window object, so it's created only once, and it stays there. So the reference to the function remains the same.

rodrigocfd
  • 6,450
  • 6
  • 34
  • 68
  • So once the function is called, it is referenced from the window object? Since reference in memory and a creation phase already exists for function declarations, then why do they say that every new execution context has a new creation phase? – Ash Nov 20 '18 at 13:08
  • Yes, the function is always referenced from `window` object. The execution context is just the scope and the scope chain (variables from other scopes in closures). Once the execution context is created, the creation phase allocates the memory for the variables inside that context. Oh, and remember that, in JS, you have function scope, not block scope. – rodrigocfd Nov 20 '18 at 13:16
  • @Rodrigo JS does have block scope since a few years now... Only `var` has function scope. – Bergi Nov 20 '18 at 13:34
  • Sorry, quick question. Are only function declarations and global variables referenced from the window object? Or do function expressions also get attached to the window object when called? Thanks. – Ash Nov 20 '18 at 13:39
  • If by "function expression" you mean arrow functions, no, they are not attached to the window object. Arrow functions are just normal variables, that can be temporary if passed directly as an argument. – rodrigocfd Nov 20 '18 at 13:57