var foo = 24; var bar = foo; foo = 34;What is the value of bar?
When foo is created it is bound to a value. foo is simply an identifier that is bound to the value 24. When we say that bar = foo, bar is being pointed to the value 24 as well. When we then assign the value of 34 to foo, foo points to 34. However, bar is still pointing to 24. The block of memory to which it is pointing has not changed.
So, in the example of above, the value of bar is 24.
Often rebinding is confused with assignment by reference. One might have incorrectly assumed that the value of bar was 34. After all, it pointed to foo and foo now pointed to the value 34. But this would be an example of assignment by reference, when what we are doing is taking identifiers and binding to values - blocks of memory.
A graphic representation of Rebinding:
Reference:
http://dmitrysoshnikov.com/ecmascript/es5-chapter-3-1-lexical-environments-common-theory/#rebinding


No comments:
Post a Comment