I have a property of ArrayTest called theArray (edited to show all of ArrayTest):
function ArrayTest(theArray = []) {
let _theArray = theArray;
Object.defineProperty(this, 'theArray', {
get: function () {
return _theArray;
},
set: function (value) {
_theArray = value;
}
});
this.theArray = theArray;
ArrayTest.prototype.toString = function () {
return this.theArray + ' ';
};
} in another module, I want to use the property theArray to return an array of numbers. The property aNumber is defined such that it is equal to itself. Therefore, I would expect the output to be [1, 2, 3], but instead, all elements get the value of the last element in theArray:
let theArray = [new TheNumber(1), new TheNumber(2), new TheNumber(3)];
let aList = new ArrayTest(theArray);
console.log(aList.toString()); //[3, 3, 3] instead of [1, 2, 3].
How can I change the definition of theArray so that the above example returns [1, 2, 3]?