Don't confuse javascript with java. By javascript your class does not have private methods, so you cannot access these functions by using the this keyword.
You can do the following:
var TestClass = function() {
var methodOne = function(a, b) {
return (a == b);
}
var methodTwo = function() {
var elements = $('.element');
elements.droppable({
drop: function(event, ui) {
if (!methodOne(ui.draggable.data('a'), ui.draggable.data('b'))) {
return false;
}
return true;
}
});
}
};
but be aware that the methodOne and methodTwo variables don't have values outside the constructor, since they are local variables of the constructor function and not methods.
You can define methods by adding them to the prototype, e.g.:
TestClass = function() {};
TestClass.prototype = {
constructor: TestClass,
methodOne: function (){...},
methodTwo: function (){...}
};
In this case the this keyword works if you call the methodOne from the methodTwo, but in your example you call it from a function defined in the methodTwo, so you have to use the bind() function to create a wrapper which sets the context by that function.
var TestClass = function() {
};
TestClass.prototype = {
constructor: TestClass,
methodOne: function(a, b) {
return (a == b);
},
methodTwo: function() {
var elements = $('.element');
elements.droppable({
drop: function(event, ui) {
if (!this.methodOne(ui.draggable.data('a'), ui.draggable.data('b'))) {
return false;
}
return true;
}.bind(this)
});
}
};
If you want to use ES6 classes instead of ES5, then the story is somewhat different, but you'll need a compiler e.g. babel, traceur, etc... As far as I know you cannot use the class keyword by ES5, so you should use var instead of that.
thisis no longer the TestClass, but specifies thethison drop(event, ui), add var self = this; below var elements, and call self.methodOne()classkeyword. It would throwUncaught SyntaxError: Unexpected token =. Did you mean to usevar/let/const?methodTwoalready has access tomethodOne, they are defined in the same scope, so you can just call it directlymethodOne(), there's no need forthis.methodOne(). Additionally, you need to research howthisgets bound in JS, depending on how a function is called, particularly when called as a constructor or an object method.