There are a few ways you can change what this refers to in a function, or otherwise retain access to the outer this. However, we should note that your problem is not with checkX, but with the anonymous function within checkX, so you need to apply one of these techniques to that function.
Storing a reference to this in a variable:
var Dom = function() {
var self = this;
this.getX = function(element, value) {
...
}
this.checkX = function (label, expectedCount, params) {
it(label + ': Check for ' + expectedCount, function () {
for (var i = 0; i < params.length; ++i) {
self.getX.apply(self, params[i]).click();
}
});
}
}
Or using Function.prototype.bind (thanks to elclanrs for mentioning this in the comments):
var Dom = function() {
this.getX = function(element, value) {
...
};
this.checkX = function (label, expectedCount, params) {
it(label + ': Check for ' + expectedCount, (function () {
for (var i = 0; i < params.length; ++i) {
this.getX.apply(this, params[i]).click();
}
}).bind(this));
}
}
..Or, by using Function.prototype.call. This technique won't work in your scenario because you don't control when the anonymous function is called, but it's useful to know in general.
var Dom = function() {
this.getX = function(element, value) {
...
};
this.checkX = function (label, expectedCount, params) {
...
}
// when calling checkX later..
this.checkX.call(this, argument1, argument2, argument3);
}
Many JavaScript libraries contain utilities to easily handle what this refers to, since callback functions are incredibly common in most JS applications. For example, see dojo.hitch or jQuery's $.proxy. You can easily write your own function to do the same:
function WrapCallbackWithContext(context, callback) {
return function() {
return callback.apply(context, arguments);
}
}