Javascript: adding methods using prototype descriptor

Posted by LDK on Stack Overflow See other posts from Stack Overflow or by LDK
Published on 2010-05-30T20:00:48Z Indexed on 2010/05/30 20:02 UTC
Read the original article Hit count: 130

Filed under:
Sorter.prototype.init_bubblesort = function(){
  console.log(this.rect_array);
  this.end = this.rect_array.length;
  this.bubblesort();
}

Sorter.prototype.init = function(array,sort_type){
  this.rect_array = array;
  this.init_bubblesort();
}

The code above works as expected, but when I change the init function to:

Sorter.prototype.init = function(array,sort_type){
  var sort_types = {'bubblesort':this.init_bubblesort,
                'quicksort':this.init_quicksort,
                'liamsort':this.init_liamsort};
  this.rect_array = array;
  sort_types[sort_type]();
}

the init_bubblesort function results in an error saying this.rect_array is undefined. I'm trying to wrap my head around why init_bubblesort no longer as access to it's instance's variables.

© Stack Overflow or respective owner

Related posts about JavaScript