0

Please explain the results of the following example, especially the cases 1.2 and 2.2:

Shouldn't be the output of case 1.2: Object[0] {foo:"a"} ?
Shouldn't be the output of case 2.2: Object[0] {foo:"b"} ?

var obj = {foo:'a'};    // Define an object
var arr = [obj];        // Store the object within an array
console.log(obj);       // Case 1.1: Object {foo:"a"}
console.log(arr);       // Case 1.2: Object[0] {foo:"c"}

function Foo(o){ o.foo = 'b'; };
Foo(obj);               // Change the object's property by using a function
console.log(obj);       // Case 2.1: Object {foo:"b"}
console.log(arr);       // Case 2.2: Object[0] {foo:"c"}

arr[0].foo = 'c';       // Change the object's property by using the array
console.log(obj);       // Case 3.1: Object {foo:"c"}
console.log(arr);       // Case 3.2: Object[0] {foo:"c"}
Pointy
  • 389,373
  • 58
  • 564
  • 602
Anne Droid
  • 3,099
  • 3
  • 15
  • 15

0 Answers0