What you need to do is to implement the memoize technique, i.e. you need a memoizer function.
Here's what Google came up with for a JS memoize implementation.
Your doing it right, only that your neglecting that functions are implemented as a type in JS--it's a first-class object, so these are completely valid statements:
function a() { };
a.foo = 'bar';
a.hasOwnProperty('foo'); // true
a.foo; // 'bar'
a = function { this.foo = 'bar' }
a.foo; // 'bar'
a['foo']; // 'bar', because objects are implemented as dictionaries
The only thing that you need to change is to set prev_numbers as a property of showItems:
function showItems() {
// bool check for undefined object properties returns false
if(!this.prev_numbers)
this.prev_numbers = '';
numbers = Math.floor(Math.random()*101);
this.prev_numbers = numbers + ',' + this.prev_numbers;
}
As to your particular problem of always receiving ReferenceError in your code, I do not know the exact implementation details, but I have observed that accessing undefined globals will raise ReferenceError instead of simply returning undefined, as you'd expect. This is how to properly handle it:
if (hasOwnProperty('prev_numbers') { ... }
// equivalent to
if(window.hasOwnProperty('prev_numbers') { ... }
Take a look at this:
baz; // ReferenceError
hasOwnProperty('baz'); // false
window.hasOwnProperty('baz') //false
baz = 'bar';
hasOwnProperty('baz'); // true
window.hasOwnProperty('baz) // true
An alternative to calling hasOwnProperty is:
foo; // ReferenceError
window.foo // undefined (no ReferenceError raised)
if (!window.foo) 'yay'; // 'yay'
if (window.foo == undefined) 'yay'; // 'yay'