-1

guys i am trying to learn some javascript and i have a question that i think is important to ask. so what i have gotten so far is that when i do something like this:

var a=function(){} 

during the creation phase there will be created in memory a variable (a) with undefined value and during the execution the a will point to the memory slot where the function lies.

so what happens to the old spot a was pointing at(the one with undefined value)? also if i set b equal to a this means they will point to the same memory slot right?

what happened to the slot that b was previously pointing at?

finally does the function gets saved to the memory during the creation phase but cant be called because nothing points at it or its just get saved during the execution phase?

thanks i hope you can help me make my mind clear (i cant sleep with these questions on my head :D)

Barmar
  • 741,623
  • 53
  • 500
  • 612

3 Answers3

1

so what happens to the old spot a was pointing at(the one with undefined value)?

I don't know the exact details of any JavaScript engines, but I suspect that there isn't really any such slot. Instead, undefined is probably just a special value that gets put into a that indicates that it doesn't point anywhere (similar to NULL in C).

If there really is a memory slot for undefined, it's a single object that all undefined variables point to. Nothing happens to it, since there are still lots of other variables pointing to it.

also if i set b equal to a this means they will point to the same memory slot right?

Yes. When you assign variables in JavaScript, it just copies the reference.

what happened to the slot that b was previously pointing at?

If anything else is still pointing to it, nothing. If nothing else is pointing to it, the garbage collector will eventually reclaim its memory.

finally does the function gets saved to the memory during the creation phase but cant be called because nothing points at it or its just get saved during the execution phase?

It gets saved when creating it. It's possible to create a function without ever assigning it to a variable -- this is a common idiom called Immediately Invoked Function Expression:

(function() { console.log("Function is running"); })();

This function is created, is invoked, and then becomes inaccessible. The garbage collector will eventually reclaim it.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

I think I understand what you are trying to ask. When you have a variable in JavaScript, or any programming language that has its own garbage collector, and you are referencing a part in memory... for instance lets say var A = 50; var B = 75;

and you want to turn around and re-assign A = B;

The memory location will be released and A will now be 75.

Here is some documentation that can explain in further detail: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management

Jay Mason
  • 446
  • 3
  • 17
0

var a=function(){}

That doesn't create a pointer to undefined and then repoints it to the function. It directly points it to the function.

Even if you did

var a;
a = function(){};

a doesn't point at a random memory place that holds undefined. A is undefined. Undefined is a value. And then you assign it with a reference pointing to the function. Undefined doesn't need to be cleared, because it's substituted.

Also, base values are copied, not referenced, so:

A = 50; var B = 75; A = B;

Doesn't use 3 memory locations, only 2, holding first 50 and 75, and then 75 and 75.

If it were objects, then it would be references. What happens to objects when they got unreferenced is that they are removed by the Garbage collector, that is a routine that frees the memory of objects no longer referenced by anyone.

jorbuedo
  • 2,041
  • 1
  • 8
  • 20