Referring to the JavaScript code snippet below, questions:
Why does the object literal {item: {value: "foobar"}} behave differently when assigned to a variable (like in line 1) vs. when passed as an argument to Object.create() (like in line 5)?
What is the difference between line 5 and line 8 - i.e. why is line 5 the correct way to pass the second argument to Object.create() and not line 8 (to override the item property in delegate)?
Code Snippet:
1 var obj = {item: {value: "foobar"}};
2 console.log(obj.item); // [object Object]
3 console.log(obj.item.value); // foobar
4 var delegate = {item: "xxx"};
5 var obj1 = Object.create(delegate, {item: {value: "foobar"}});
6 console.log(obj1.item); // foobar
7 console.log(obj1.item.value); // undefined
8 var obj2 = Object.create(delegate, {item: "bar"});
9 console.log(obj2.item); // <nothing>