Documentos de Académico
Documentos de Profesional
Documentos de Cultura
summary, setting one variable equals to another by copying the value. 2 variables
become the same by copying the same value into 2 separate spots in memory.
BY REFERENCE
if we have an object instead...
a. when I set a variable equal to an object. we still get a location, an address in
memory so that the variable knows where that object lives. that's how we reference
it.
b. b=a or pass to a function.
b, instead of getting a new location in memory, simply points to the same location
in memory that a does. no new object is created. no copy of the original object is
created. instead, two names point to the same address. it's just like an alias. you
have two names, it doesn't matter because you live in the same place. both a and b
have the same value because when you ask a and b they point to the same location in
memory.
ie. BY VALUE
var a = 3;
var b;
b=a;
a=2//you can change a without affecting b
console.log(a);
console.log(b);
output:
2
3
d=c; //equals operator sees an object and points both names to the same spot in
memory.
c.greeting = 'hello';//mutate
output:
object hello
object hello
output:
object Hola
object Hola
addt'l code:
//equals operator sets up new memory space (new address)
c = {greeting:'howdy'};
console.log(c);
console.log(d);
//d and c will no,longer be pointing to the same object in memory. (initialization?
first decalration?)
*in some languages, by reference and by values are in control by developers. In JS,
we do not have that option. All primitive types are by value and all objects are by
reference.