I'm trying a more object oriented approach with javascript and have implemented a class with some methods through the use of class.prototype.
But I have a problem.
I tried to use a method from myclass as the success function of an ajax call. The issue is I can't use this withing that method when is called back by ajax. I.E:
MyClass.prototype.myMethod = function(data)
{
this.data = data; /* in here this is the window object */
}
var myClass = new MyClass();
$.ajax:
success:myClass.myMethod;
Am I doing something wrong?
Edit: Full function to try
am not i am
method
function MyClass()
{
this.name="myclass";
};
MyClass.prototype.print = function()
{
alert(this.name);
};
var myAjax = function(context_,func)
{
$.ajax({
url:"www.google.com",
type: "GET",
context:context_,
complete:function(data){
func(data);
}
});
};
var refreshGroups = function(groups)
{
var myClass = new MyClass();
myAjax(myClass,myClass.print);
return;
}
Result: alert is empty
$.ajax({ success:myClass.myMethod; });?myMethodfunction doesn't know it's meant to only act on your class, it's just a method. You will need to wrap the call in an anonymous function that calls the method on your object. ReplacemyClass.myMethodwithfunction(){myClass.myMethod();}thisin a weird way.