Javascript Namespacing And This/that Scope
Solution 1:
JavaScript is pass/call/assign by value. That means that a variable or property will always contain a value, but never a reference to a variable. I.e. assigning a new value to a variable or property does not change the value of another variable or property (exceptions are global scope and with
scopes, where the variable bindings are connected to an object).
Example:
var foo = 42;
var bar = foo;
foo = 21;
console.log(bar); // 42
bar
does not have a reference to foo
. Instead the value of foo
is copied to bar
. Thus changing foo
or bar
later on does not impact the other variable.
Compare that to PHP where we can actually assign a variable by reference using a special operator:
$foo = 42;
$bar = &$foo;
$foo = 21;
echo$bar; // 21
Applied to your example:
When you do that.letter = letter
, you are copying the value of letter
to that.latter
. Now both bindings have the same value.
Later when you assign letter = "B";
and that.letter = "C";
, you are assigning new values to the variable and the property and now they have different values.
I might be missing something from your question, but I don't see how the issue has anything to do with this
or namespaces in particular.
Post a Comment for "Javascript Namespacing And This/that Scope"