Primitives
var a = 23;
var b = a;
a = 46;
console.log(a);//46
console.log(b);//23
//two variable holding two differrent things
Objects
var obj1 = {
name: 'John',
age: 26
};
var obj2 = obj1;
obj1.age = 30;
console.log(obj1.age);//30
console.log(obj2.age);//30
// both variable holding same reference that point to exact same object in a memory.
Primitives vs Objects in Functions
var age = 27;
var obj ={
name: 'Jonas',
city: 'Lisbon'
};
function change(a, b){
a = 30;
b.city = 'LA';
}
change(age,obj);
console.log(age);//27
console.log(obj.city);//LA
// we can change variable in function as much as we want
// but it would not effected variable outside of function.
// cuz it's primitives.
but when we pass the obj, we don't actually obj. we only pass reference of obj.
so when we change the obj thru function it is still reflicted outside of function.
'javascript' 카테고리의 다른 글
JavaScript/ Immediately Invoked Function Expressions [IIFE] (0) | 2020.02.24 |
---|---|
JavaScript/ First-Class Functions (0) | 2020.02.24 |
JavaScript/ Function constructor (0) | 2020.02.24 |
JavaScript/ The 'this' keyword (0) | 2020.02.24 |
JavaScript/ Scoping/ 변수영역 (0) | 2020.02.24 |