[英]JS returning function not present
I have following code. 我有以下代码。
var handleJson = function(){
var jsonData ='xx';
var eventObject,eventObjectx=[];
that = this;
var convertJsonToObject = function(datax){
//debugger;
try {
that.printList(datax.data);
} catch(e) {
console.log(e);
}
//debugger;
}
return{
getDatafromserver: function(url){
$.ajax({
crossOrigin: true,
url: url,
success:convertJsonToObject
//console.log(jsonData);
});
},
printList:function(eventObject){
$.each(eventObject,function(index,val){
$('#eventlist').append('<li>'+val.event+'</li>');
})
}
}
}
var jsonHandler = new handleJson();
jsonHandler.getDatafromserver(url);
//jsonHandler.printList('eventlist');
});
Although the function printList
exist its returning error 尽管函数
printList
存在,但返回错误
TypeError: that.printList is not a function {stack: (...), message: "that.printList is not a function"}
TypeError:that.printList不是函数{stack:(...),消息:“ that.printList不是函数”}
Can any one help me out? 谁能帮我吗?
The value of this
as of your that = this
line is not in any way related to the object that you return at the end of your handleJson
function. 从
that = this
行开始的this
值与您在handleJson
函数末尾返回的对象没有任何关系。
Since you're using new
with handleJson
, you want to add to the object that this
refers to, rather than returning a new, unrelated object: 由于您使用
new
与handleJson
,您要添加的对象, this
是指,而不是返回一个新的,不相关的对象:
var handleJson = function() {
var jsonData = 'xx';
var eventObject, eventObjectx = [];
var that = this;
var convertJsonToObject = function(datax) {
//debugger;
try {
that.printList(datax.data);
} catch (e) {
console.log(e);
}
//debugger;
}
this.getDatafromserver = function(url) {
$.ajax({
crossOrigin: true,
url: url,
success: convertJsonToObject
//console.log(jsonData);
});
};
this.printList = function(eventObject) {
$.each(eventObject, function(index, val) {
$('#eventlist').append('<li>' + val.event + '</li>');
})
};
};
var jsonHandler = new handleJson();
jsonHandler.getDatafromserver(url);
//jsonHandler.printList('eventlist');
Here's how new
works: 这是
new
工作原理:
prototype
property refers to; prototype
属性引用的对象支持; so in your case, a new object backed by handleJson.prototype
. handleJson.prototype
支持的新对象。 handleJson
) with this
referring to that new object. handleJson
用) this
指的是新的对象。 null
, new
takes the object that it created as its result; null
,则new
将其创建的对象作为结果;否则,返回null
。 but if the target function returns a non- null
object reference, that overrides that default and new
takes that object reference as its result. null
对象引用,则它将覆盖该默认值,而new
将该对象引用作为其结果。 In your original code, that last point was coming into play. 在您的原始代码中,最后一点正在发挥作用。
Side note: Your code was falling prey to The Horror of Implicit Globals becuse you weren't declaring that
. 旁注: 由于您没有声明
that
您的代码成为“隐式全球性恐怖”的牺牲品。 I've added var
on it above. 我在上面添加了
var
。
Side note: The overwhelming convention in JavaScript is that a constructor function (a function you're calling via new
) starts with a capital letter. 旁注:JavaScript中的压倒性约定是构造函数 (您通过
new
调用的函数)以大写字母开头。 They're also usually nouns rather than verbs (other kinds of functions are usually verbs, but not constructor functions, which are named after the thing they construct). 它们通常也是名词,而不是动词 (其他类型的函数通常是动词,但不是构造函数,后者以其构造的事物命名)。 So
JsonHandler
rather than handleJson
. 所以是
JsonHandler
而不是handleJson
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.