'undefined' - if it's defined as a primitive value, what is it defined in terms of its value at the memory level?

半城伤御伤魂 提交于 2019-12-12 04:07:28

问题


I understand from MDN that 'undefined' is recognised as a primitive value, which is corroborated by the ES doco also stating that an "undefined value" is a "primitive value used when a variable has not been assigned a value".

I also understand even though the variable may not be assigned a value (i.e. an uninitialised variable), memory is still allocated for it during the creation of its execution context ('creation' phase) prior to execution happening. This explains why when we attempt to access the variable, we do not get a reference error - rather we just encounter 'undefined' (i.e. in the console log).

Noting the above, my question is, in memory, what does this look like at the memory location of the variable? Is there actually a value at the allocated memory location/address of the undefined variable? Or is the value at the memory address empty (nothing there)? If so, could we then describe the value as null (0x00)?

Thanks.


回答1:


I also understand even though the variable may not be assigned a value (i.e. an uninitialised variable), memory is still allocated for it during the creation of its execution context ('creation' phase) prior to execution happening. This explains why when we attempt to access the variable, we do not get a reference error - rather we just encounter 'undefined'.

No. An uninitialised variable is something different than a variable initialised with the value undefined. Have a look at this answer explaining the initialisation for various kinds of declarations.

What does this look like at the memory location of the variable?

It doesn't really matter how this is implemented. Every implementation may do it different, what matters are the observable effects in JS.

Is there actually a value at the allocated memory location/address of the variable, or is the value at the memory address empty?

Allocated memory always holds some value. It might be a value for which no representation exists in JS, though.



来源:https://stackoverflow.com/questions/46013456/undefined-if-its-defined-as-a-primitive-value-what-is-it-defined-in-terms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!