The prototype is an object that is inherited? by all instances of the object, like child in my following example.
It has to be an instance of the parent, else parent's prototype will not get inherited?
In this case, the goal is to create a separate array, which is inherited from parent, for each instance of child.
I am unsure how to achieve that exactly. I know of extend.
Is the extend method simply copying prototypes over to a new object and applying the new methods onto it as well?
My code example + jsfiddle:
function parent(){
}
parent.prototype.arr = [];
parent.prototype.add = function(item){
this.arr.push(item);
}
parent.prototype.show = function(){
for(var i = 0; i < this.arr.length; ++i){
$('body').append(this.arr[i]);
}
}
function child(childname){
this.add(childname);
}
child.prototype = new parent();
var child1 = new child('child1');
var child2 = new child('child2');
child2.show();//this should only show child2?