I have a JavaScript object called ShippingUI:
function ShippingUI(){
...
}
It has several methods:
ShippingUI.prototype.UpdateItemQTYs = function(ItemJQOBJ, NewQTY)
{
...
}
ShippingUI.prototype.EH_SortableRecieve = function(event, ui)
{
...
}
The “EH_SortableRecieve()” function is a drop event handler. When it runs, it needs to call “UpdateItemQTYs()”, a sister function in the same object. I’m trying to use:
ShippingUI.prototype.EH_SortableRecieve = function(event, ui)
{
this.UpdateItemQTYs('ABCD',123);
}
But keep getting the error:
"this.UpdateItemQTYs is not a function"
I’m guessing that “this” is pointing to something else… so how do I get the ‘real’ “this”?
Event Binding method:
// Takes a Jquery Object and makes it sortable with our custom options
this.MakeSortable = function(JQOBJ)
{
JQOBJ.sortable({
connectWith: '.connectedSortable',
items: '.ItemLineWrapper',
dropOnEmpty: true,
axis: 'y',
receive: this.EH_SortableRecieve
});
}
There is something missing in your examples which is how EH_SortableRecieve is called. But based on what you say it should be used as I’m thinking it is used something like this:
In which case you should be aware of Javascript’s binding of
thisin methods. Specifacally, in event handlersthisis bound to thewindowobject instead of the object the method belongs to. This is a general feature of javascript: methods can be re-bound at runtime. In other words, objects can steal other object’s methods. For example, I can have my objectslebetmans_objectsteal your method and re-bind itsthiswith the following:There are several strategies to get around this. You can use a closure to capture your object:
You can use a closure in the object’s constructor to capture the correct reference to your object:
There are probably other ways to do this but these are the two most common that I personally use.