I'll explain this thoroughly
o={a:1};
first you set global variable o to be a reference of new anonymous object that have attribute variable a with value = 1 name this object {a:1} as '1A'
change(o);
now you call function change and javascript check of typeof variable o and it's 'object'
actually it's should be 'reference that pointed to object' so the reference of object {a:1} is pass into function change by the way if variable is primitive it will pass only by value
function change(myObj){
now function change create variable myObj with typeof 'undefined' by default and then change to 'object' because it got reference parameter and now the variable myObj is a reference variable that pointed to object '1A' => {a:1} and myObj is visible only in function change
and global variable o maybe still point to object '1A' => {a:1} if myObj is just a copy of reference to object '1A' => {a:1} by language design
myObj={};
now the reference variable myObj is point to new anonymous empty object {} name this object as '2B'
myObj.a=2;
}
now you use reference myObj to set object '2B' => {} to have new attribute a with value = 2
and end the scope of function that mean global can't see object '2B' => {a:2}
alert(o.a); //alert 1
variable still point or may be point back to object {a:1} ,reference o can't lose it point,
because object '2B' => {a:2} can't be seen outside function change
and will be destroyed by garbage collection because it's lost the reference
and object '1A' => {a:1} can't be destroyed by garbage collection
because variable o still point at it that why you call o you receive object '1A' => {a:1}
sorry for my bad grammar but I try my best to make it easy to read.