I have created collection base class/object to do the repeated task. I have CollectionBase, and I have Persons class that inherit to CollectionBase. but my problem is when i create 2 persons collection like persons1 = new Persons() and persons2 = new Persons() it seems that they have same reference object. Any suggestion on how i can make everytime i will create a new Persons it will create new instance.
Please refer to this on plnkr; http://plnkr.co/edit/8GPkWO4nKRiskxoRWrkg?p=info
(function(){
function CollectionBase(){
this.collection = [];
}
Object.defineProperties(CollectionBase.prototype, {
count: {
get: function(){
return this.collection.length;
},
enumerable: true
}
});
CollectionBase.prototype.init = function(){
};
CollectionBase.prototype.get = function(index){
if (index === undefined){
return this.collection;
}
return this.collection[index];
};
CollectionBase.prototype.remove = function(index){
try{
if (index === undefined) throw new Error('index is undefined');
this.collection.splice(index, 1);
}
catch(err){
console.log(err);
}
};
CollectionBase.prototype.update =function(item){
};
CollectionBase.prototype.add = function(item){
this.collection.push(item);
};
function Person(firstName, lastName){
this.firstName = firstName;
this.lastName = lastName;
}
function Persons(){
CollectionBase.call(this);
}
Persons.prototype = Object.create(CollectionBase.prototype);
var persons1 = new Persons();
var persons2 = new Persons();
})();