Can somebody explain what is going on here: code sample 1:
o2={b:10};
function classCreate(proto,o){
return Object.create(proto,o);
}
var o1=classCreate({a:o2},{});
console.log(o1.a.b); // prints 10
code sample 2:
o2={b:10};
function classCreate(proto,o){
return Object.create(proto,o);
}
var o1=classCreate({},{a:o2});
console.log(o1.a.b); //reference error
How does Object.create manage to mangle its scope when extending the final object?
The second argument in
Object.create
should be an object of properties given in a very strict format. For example this would work:Read more about it here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties