Javascript 如何在ajax回调函数中调用方法?
我试图在回调函数中调用一个方法Javascript 如何在ajax回调函数中调用方法?,javascript,ajax,Javascript,Ajax,我试图在回调函数中调用一个方法 jsTest.prototype.getTitle = function() { thisJS=this //the console will returns my object...so it's not undefined. console.log(thisJS); //codes..... //ajax callback function ajaxcall.callback=function(data){
jsTest.prototype.getTitle = function() {
thisJS=this
//the console will returns my object...so it's not undefined.
console.log(thisJS);
//codes.....
//ajax callback function
ajaxcall.callback=function(data){
//call the addName method
thisJS.addName(data, 1);
};
}
jsTest.prototype.addName=function(data, bool){
console.log(data);
}
我说了一个错误
Uncaught TypeError: Cannot call method 'addName' of undefined
有没有办法解决这个问题?非常感谢 试试换衣服
thisJS=this
为此:
var thisJS=this
要调用addName方法,必须像在OOP中一样引用该类 如果没有创建jsTest对象,则不能使用addName
var item = new jsTest();
item.addName(data, 1);
您能否显示调用
getTitle
的代码?“codes…”中的任何代码是否访问thisJS?尝试将thisJS
定义为局部变量:var thisJS=this代码>。我认为prototype仅适用于使用new.创建的实例。如果我错了,请指出!!var thisJS=new jsTest()@Sushanth——原型是所有函数的属性。@Matt。。我认为原型只能用于由new关键字创建的构造函数!!