我试图创建某种ajax连接检查对象,但由于某种原因,我只是不能让它工作:/
对象:function conn(data){
this.conn = false,
this.check = function(data){
if(data){
console.log("data: "+data)//Returns true
this.conn = data;
} else {
this.callServer();
}
},
this.callServer = function(){
$.get(ApiUrl("online"))
.done(function(data){})
.fail(function(data){})
.always(function(data){var c = new conn();c.check(data)});
}
}
用法:
var code = $(".login-form input#pincode").val();
var con = new conn();
con.check();
console.log("connection: "+ con.conn); // returns false
code = code.match(/[0-9]/g);
if (code === null || code.length !== 6) {
Alert("De code moet bestaan uit zes cijfers");
} else {
if(con.conn === true){
//Do stuff
}
函数'ApiURL'是创建url的函数,所以不要介意
您在使用对象构造函数创建新对象时丢失了"数据"。var con = new conn();
需要是var con = new conn(<data need to be inserted here>);
,我想这就是为什么它不起作用。