在没有类封装的情况下,代码什么问题都么有:
function startRequest(xmlHttp, sUrl, data) {
try {
xmlHttp.open("GET", sUrl + "temp=" + (Math.random()), true);
xmlHttp.setRequestHeader("Content-Length", data.length);
xmlHttp.onreadystatechange = function () {
readdata(xmlHttp);
};
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send(data);
}
catch (exception) {
alert(exception);
}
}function readdata(xmlHttp){
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var result = xmlHttp.responseText.replace(/(\r\n)+/g, "");
}
}
}但当我用MyObject()封装了代码。但跑到
xmlHttp.onreadystatechange = function () {
this.readdata(xmlHttp);
}
这个位置时就出报错了。
请问在类中,这里应该怎么写呢?
function MyObject(){ this.startRequest = function (xmlHttp, sUrl, data) {
try {
xmlHttp.open("GET", sUrl + "temp=" + (Math.random()), true);
xmlHttp.setRequestHeader("Content-Length", data.length);
HXML = xmlHttp
xmlHttp.onreadystatechange = function () {
this.readdata(xmlHttp);
};
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send(data);
}
catch (exception) {
alert(exception);
}
}; this.readdata = function (xmlHttp) {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var result = xmlHttp.responseText.replace(/(\r\n)+/g, "");
}
}
}
}
function startRequest(xmlHttp, sUrl, data) {
try {
xmlHttp.open("GET", sUrl + "temp=" + (Math.random()), true);
xmlHttp.setRequestHeader("Content-Length", data.length);
xmlHttp.onreadystatechange = function () {
readdata(xmlHttp);
};
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send(data);
}
catch (exception) {
alert(exception);
}
}function readdata(xmlHttp){
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var result = xmlHttp.responseText.replace(/(\r\n)+/g, "");
}
}
}但当我用MyObject()封装了代码。但跑到
xmlHttp.onreadystatechange = function () {
this.readdata(xmlHttp);
}
这个位置时就出报错了。
请问在类中,这里应该怎么写呢?
function MyObject(){ this.startRequest = function (xmlHttp, sUrl, data) {
try {
xmlHttp.open("GET", sUrl + "temp=" + (Math.random()), true);
xmlHttp.setRequestHeader("Content-Length", data.length);
HXML = xmlHttp
xmlHttp.onreadystatechange = function () {
this.readdata(xmlHttp);
};
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send(data);
}
catch (exception) {
alert(exception);
}
}; this.readdata = function (xmlHttp) {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var result = xmlHttp.responseText.replace(/(\r\n)+/g, "");
}
}
}
}
解决方案 »
- 修改保存代码该怎么写
- 利用闭包实现私有成员的单体(Singleton),访问内部成员时出现的差别,不知道为何
- 关于Jquery的change事件
- 图片上放一个透明小层,图片翻转,层也翻转。翻转后 层变的不透明了
- w3cschool 关于xml例题的问题
- JS实现简单拖拽
- ==== JAVASCRIPT 能否设置Session??? ====
- javascript 弹出窗口问题
- 请问那种text输入框或表格的边框是虚线的效果是怎么做的,有源代码吗?谢谢!
- 随机生成100000个数字,插入一个新数组,最快的写法
- 如何让自己的JavaScript程序有安全的签名?
- 大家帮帮忙,JAVASCRIPT问题(刷新后的父窗口如何管理刷新前子窗口)
this的问题,这里的this引用的是window
不信将
xmlHttp.onreadystatechange = function () {
this.readdata(xmlHttp);
}
改成
xmlHttp.onreadystatechange = function () {
alert(this==window);//应该是true
this.readdata(xmlHttp);
}
try {
xmlHttp.open("GET", sUrl + "temp=" + (Math.random()), true);
xmlHttp.setRequestHeader("Content-Length", data.length);
xmlHttp.onreadystatechange = function () {
readdata();
};
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send(data);
}
catch (exception) {
alert(exception);
}
}function readdata(){
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var result = xmlHttp.responseText.replace(/(\r\n)+/g, "");
}
}
}
阁下的方法lz早就会的,lz问的是怎么封装to lz:
解决参数问题,一般可以用闭包
function MyObject(){ this.startRequest = function (xmlHttp, sUrl, data) {
try {
xmlHttp.open("GET", sUrl + "temp=" + (Math.random()), true);
xmlHttp.setRequestHeader("Content-Length", data.length);
HXML = xmlHttp
xmlHttp.onreadystatechange = this.bibao(this);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send(data);
}
catch (exception) {
alert(exception);
}
}; this.readdata = function (xmlHttp) {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var result = xmlHttp.responseText.replace(/(\r\n)+/g, "");
}
}
}
this.bibao=function (obj){
return function (){obj.readdata(xmlHttp);};
};
}
看起来这种写法成功机会挺大。我再试试!谢谢老兄。