In node.js, I am struggling with classes / objects and the keyword 'this'. For instance:
function Set(arr,leq) { this.leq = leq ? leq : function(x,y) {return x<=y;}; this.arr=arr.slice().sort(this.geq); } Set.prototype.geq=function(x,y) { return this.leq(y,x);} var myset = new Set([1,5,3,2,7,9]); TypeError: Object
However, if I remove the .sort fragment as follows:
function Set(arr,leq) { this.leq = leq ? leq : function(x,y) {return x<=y;}; this.arr=arr.slice(); }
I have no problem with:
var myset = new Set([1,5,3,2,7,9]); myset.geq(3,4) false
but still:
myset.arr.sort(myset.geq) TypeError: Object #<Object> has no method 'leq' at ...
So: How to create a method (e.g. geq ) in my object that has access to another method (e.g. leq ) in the same object when I need to access the first method, for example. sort function?
source share