我现在copy一段代码,大家帮我看下这里的call方法的作用是什么啊?
js代码如下:
var net=new Object();
net.AjaxRequest=function(url,onload,onerror,method,params){
this.req=null;
this.onload=onload;
this.onerror=(onerror) ? onerror : this.defaultError;
this.loadDate(url,method,params);
}
net.AjaxRequest.prototype.loadDate=function(url,method,params){
if (!method){
method="GET";
}
if (window.XMLHttpRequest){
this.req=new XMLHttpRequest();
} else if (window.ActiveXObject){
try{
this.req=new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
this.req=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){}
}
}
if (this.req){
try{
var loader=this;
this.req.onreadystatechange=function(){
net.AjaxRequest.onReadyState.call(loader); 问题1:这里的call是什么意思啊????
}
this.req.open(method,url,true);
if(method=="POST"){
this.req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
}
this.req.send(params);
}catch (err){
this.onerror.call(this); 问题2:这里的call是什么意思啊?
}
}
}net.AjaxRequest.onReadyState=function(){
var req=this.req;
var ready=req.readyState;
if (ready==4){
if (req.status==200 ){
this.onload.call(this); 问题3:这里的call方法什么意思?
}else{
this.onerror.call(this); 问题4:这里的call方法什么意思?
}
}
}net.AjaxRequest.prototype.defaultError=function(){
alert("错误数据\n\n回调状态:"+this.req.readyState+"\n状态: "+this.req.status);
}
js代码如下:
var net=new Object();
net.AjaxRequest=function(url,onload,onerror,method,params){
this.req=null;
this.onload=onload;
this.onerror=(onerror) ? onerror : this.defaultError;
this.loadDate(url,method,params);
}
net.AjaxRequest.prototype.loadDate=function(url,method,params){
if (!method){
method="GET";
}
if (window.XMLHttpRequest){
this.req=new XMLHttpRequest();
} else if (window.ActiveXObject){
try{
this.req=new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
this.req=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){}
}
}
if (this.req){
try{
var loader=this;
this.req.onreadystatechange=function(){
net.AjaxRequest.onReadyState.call(loader); 问题1:这里的call是什么意思啊????
}
this.req.open(method,url,true);
if(method=="POST"){
this.req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
}
this.req.send(params);
}catch (err){
this.onerror.call(this); 问题2:这里的call是什么意思啊?
}
}
}net.AjaxRequest.onReadyState=function(){
var req=this.req;
var ready=req.readyState;
if (ready==4){
if (req.status==200 ){
this.onload.call(this); 问题3:这里的call方法什么意思?
}else{
this.onerror.call(this); 问题4:这里的call方法什么意思?
}
}
}net.AjaxRequest.prototype.defaultError=function(){
alert("错误数据\n\n回调状态:"+this.req.readyState+"\n状态: "+this.req.status);
}
<script>
function myFun(p1, p2, p3)
{
this.para1 = p1;
this.para2 = p2;
this.para3 = p3;
}var myObject = new Object(); alert(myObject.para1) //显示undefinedmyFun.call(myObject,3,5,7);
/*执行的时候,myFun方法中的this全部用myObject对象代替,所以在这个例子中,执行myFun方法和直接写下面的语句效果是一样的:
myObject.para1 = 3;
myObject.para2 = 5;
myObject.para3 = 7;
*/
alert(myObject.para1) //显示3
alert(myObject.para2) //显示5
</script>
this.onload.call(this); 问题3:这里的call方法什么意思?//
注意这里是net.AjaxRequest.onReadyState=function()这个环境下,也有了onload方法
}else{
this.onerror.call(this); 问题4:这里的call方法什么意思?//这个环境下也有了onerror方法
}
回应了顶上的这2句:
this.onload=onload;
his.onerror=(onerror) ? onerror : this.defaultError;
<script type="text/javascript">
var lisi = {
name:'李四',
famu:function() {
alert(this.name+'在伐木')
}
}var zhangsan = {
name:'张三'
}lisi.famu.call(zhangsan);
lisi.famu();
// 下面错误,call相当于临时赋予,并不是真正具有
zhangsan.famu();
</script>