I want constructor Paper
to inherit constructor View
. I've read that there needs to be a temporary constructor new F()
, but the parent is modified along with the child class prototype in my code:
function View() {};
function Paper() {};
View.prototype = {
location: {
"city": "UK"
}
}
function F() {};
F.prototype = View.prototype;
Paper.prototype = new F();
Paper.prototype.constructor = Paper;
So when I try to modify the Paper
's prototype:
Paper.prototype.location.city = "US";
I find the View
's prototype is modified too!:
var view = new View();
console.log(view.location); //US! not UK
So what's wrong with my code? How can I override the prototype without affecting the parent?