Using Crockford's method ( http://javascript.crockford.com/prototypal.html ) doesn't work when we have reference values in properties (an array in the supertype) as this is common among all the objects.
So, what's really the recommended way to do object inheritance in Javascript that doesn't have problem and that visually encapsulates better all the object's methods and properties?
Example:
if (typeof Object.create !== 'function') {
Object.create = function (o) {
function F() {}
F.prototype = o;
return new F();
};
}
var p1 = {name: "nick", friends : ["a1", "a2"]};
var p2 = Object.create(p1);
p2.name ="john";
p2.friends.push("andre");
alert(p1.friends);
alert(p2.friends);
alert(p1.name);
alert(p2.name);
The friends array returns the same values for both p1 and p2, while I expect that it returns different values.