javascriptvisualizer icon indicating copy to clipboard operation
javascriptvisualizer copied to clipboard

Object Factories (as well as Constructor Inheritance) not possible to demonstrate

Open mstolze opened this issue 7 years ago • 0 comments

var results = [];
function makeCounterObj(start) {
  var counter = start;
  return {
    getNext: function() {
                    return counter++;}
  };
}

var counter1 = makeCounterObj(1);
var counter10 = makeCounterObj(10);

results.push(counter1.getNext());
results.push(counter10.getNext());
results.push(counter1.getNext());
results.push(counter1.getNext());
results.push(counter10.getNext());

When above is run, stepping into getNext does not work. Same when a Constructor/New is used.

function Counter(start) {
  var counter = start;
  this.getNext = function() {
                    return counter++;};
}
var counter1 = new Counter(1);
...
screenshot

mstolze avatar Oct 13 '18 20:10 mstolze