I'm not sure why js is sharing an array between two instances and not other properties. Anyone care to help with this one? I've tried it without using the prototype, as well. I get the same thing
function ItemScroller() {};
ItemScroller.prototype = {
// var ItemScroller = {
items: [],
itemCount: 0,
position: 0,
InsertItem: function( item ) {
this.items.push( item );
this.itemCount++;
},
DisplayPosition: function() {
this.items[this.position].style.display = "block";
},
NextItem: function() {
if ( this.position < this.itemCount - 1 ) {
this.items[this.position].style.display = "none";
this.position++;
this.items[this.position].style.display = "block";
}
},
PreviousItem: function() {
if ( this.position != 0 ) {
this.items[this.position].style.display = "none";
this.position--;
this.items[this.position].style.display = "block";
}
},
};
Using the above definition,
var one = new ItemScroller();
//var one = Object.create(ItemScroller);
var two = new ItemScroller();
//var two = Object.create(ItemScroller);
for ( var i = 0; i < 4; i++) {
one.InsertItem("sick");
}
for ( var i = 0; i < 2; i++) {
two.InsertItem("ok");
}
Then displaying the contents of each --
console.log(one.itemCount); //output: 4
console.log(one.items); //output: [sick, sick, sick, sick, ok, ok]
Above prints 4 and all of the six items in one's array
console.log(two.itemCount); //output: 2
console.log(two.items); //output: [sick, sick, sick, sick, ok, ok]
Above prints 2 and all of the six items in two's array
I'm expecting the following --
console.log(one.itemCount); //output: 4
console.log(one.items); //[sick, sick, sick, sick]
and
console.log(two.itemCount); //output: 2
console.log(two.items); //output: [ok, ok]