Javascript: adding methods using prototype descriptor
- by LDK
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.