关于javascript引用父级变量的问题 本帖最后由 joulang 于 2012-01-30 07:48:13 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 function myClass(){ this.a = "origin"; }myClass.prototype = { b:function(){ function d(obj){ alert(obj.a); obj.a='new'; } d(this); alert(this.a); }}var obj = new myClass();obj.b(); function myClass(){ this.a = "fuck you"; //这里定义成员a给下面的函数使用}myClass.prototype = { b:function(){ var that = this; function d(){ alert(that.a); //请问这里如果引用上面定义的变量a弹出fuckyou?同时还要在这里用this.a = "shit";达到修改上面的变量a的效果 } d(); }}var obj = new myClass();obj.b(); obj.prototype={func_name:function(){/...}}prototype还能这么写?学习了 尽量不要通过参数解决,因为我是通过类似句柄来引用函数,我贴上我问题的全部代码:function AjaxClass(){ this.URL = ""; this.Type = ''; this.XHR = this.Create() ; this.Result = '';}AjaxClass.prototype = { Create:function(){ var xmlHttp = false; if(window.XMLHttpRequest){ //在非IE中创建XMLHttpRequest对象 xmlHttp = new XMLHttpRequest(); }else if(window.ActiveXObject){ try{ xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); //高版本IE }catch(error1){//创建IP高版本XMLHttpRequest对象失败 try{ xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); //低版本IE }catch(error2){ //创建IP低版本XMLHttpRequest对象失败 xmlHttp = false; } } } if(!xmlHttp){ alert("创建XMLHttpRequest对象失败!程序无法运行,请检查您的浏览器类型与版本!"); return false; } return xmlHttp; }, ResponseHandle:function(){ alert(this.XHR); //关键之处:问题出在这里,通过this引用的成员比如this.XHR都会显示undefined,我要的就是能正常引用上面的成员。 if(this.XHR.readyState == 1){this.Result = "正在等待执行...";} if(this.XHR.readyState == 4 || this.XHR.readyState == "complete"){ alert("等于4啦"); if(this.XHR.status == 200){ if(this.Type == 'XML'){ this.Result = this.XHR.responseXML; }else{ this.Result = this.XHR.responseText; } } } }, Get:function(TagetURL,ResponseType){ this.URL = TagetURL+"&randcode="+(new Date()).valueOf(); if(ResponseType != undefined && TagetURL != undefined){ this.Type = ResponseType.toUpperCase(); } if(window.XMLHttpRequest){ this.XHR.open("get",this.URL); this.XHR.onreadystatechange = this.ResponseHandle; //关键之处,问题源头在这里。通过这里引用this.ResponseHandle函数,注意这里不能写成this.ResponseHandle(),否则没效果,所以没有()就不能传送参数了。 this.XHR.send(null); }else{ this.XHR.open("get", this.URL, true); this.XHR.onreadystatechange = this.ResponseHandle; this.XHR.send(); } } }var obj = new AjaxClass();obj.Get("ajax.php?msg=fuckyou"); function Bind(obj,fun) { return function () { fun.apply(obj, [1, 2]); //[1, 2]是传给ResponseHandle的参数,见过别人这样取参数的var args = Array.prototype.slice.call(arguments); } } function AjaxClass() { this.URL = ""; this.Type = ''; this.XHR = this.Create(); this.Result = ''; } AjaxClass.prototype = { Create: function () { var xmlHttp = false; if (window.XMLHttpRequest) { //在非IE中创建XMLHttpRequest对象 xmlHttp = new XMLHttpRequest(); } else if (window.ActiveXObject) { try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); //高版本IE } catch (error1) {//创建IP高版本XMLHttpRequest对象失败 try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); //低版本IE } catch (error2) { //创建IP低版本XMLHttpRequest对象失败 xmlHttp = false; } } } if (!xmlHttp) { alert("创建XMLHttpRequest对象失败!程序无法运行,请检查您的浏览器类型与版本!"); return false; } return xmlHttp; }, ResponseHandle: function () { //alert(this.XHR); 关键之处:问题出在这里,通过this引用的成员比如this.XHR都会显示undefined,我要的就是能正常引用上面的成员。 if (this.XHR.readyState == 1) { this.Result = "正在等待执行..."; } if (this.XHR.readyState == 4 || this.XHR.readyState == "complete") { debugger; alert("等于4啦"); if (this.XHR.status == 200) { if (this.Type == 'XML') { this.Result = this.XHR.responseXML; } else { this.Result = this.XHR.responseText; } } } }, Get: function (TagetURL, ResponseType) { this.URL = TagetURL + "&randcode=" + (new Date()).valueOf(); if (ResponseType != undefined && TagetURL != undefined) { this.Type = ResponseType.toUpperCase(); } if (window.XMLHttpRequest) { this.XHR.open("get", this.URL); this.XHR.onreadystatechange = Bind(this,this.ResponseHandle);//关键之处,问题源头在这里。通过这里引用this.ResponseHandle函数,注意这里不能写成this.ResponseHandle(),否则没效果,所以没有()就不能传送参数了。 this.XHR.send(null); } else { this.XHR.open("get", this.URL, true); this.XHR.onreadystatechange = this.ResponseHandle; this.XHR.send(); } } } var obj = new AjaxClass(); obj.Get("TreeHandler.ashx?id=0"); function Bind(obj,fun) { return function () { fun.apply(obj, [1, 2]); //[1, 2]是传给ResponseHandle的参数,见过别人这样取参数的var args = Array.prototype.slice.call(arguments); } } function AjaxClass() { this.URL = ""; this.Type = ''; this.XHR = this.Create(); this.Result = ''; } AjaxClass.prototype = { Create: function () { var xmlHttp = false; if (window.XMLHttpRequest) { //在非IE中创建XMLHttpRequest对象 xmlHttp = new XMLHttpRequest(); } else if (window.ActiveXObject) { try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); //高版本IE } catch (error1) {//创建IP高版本XMLHttpRequest对象失败 try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); //低版本IE } catch (error2) { //创建IP低版本XMLHttpRequest对象失败 xmlHttp = false; } } } if (!xmlHttp) { alert("创建XMLHttpRequest对象失败!程序无法运行,请检查您的浏览器类型与版本!"); return false; } return xmlHttp; }, ResponseHandle: function () { //alert(this.XHR); 关键之处:问题出在这里,通过this引用的成员比如this.XHR都会显示undefined,我要的就是能正常引用上面的成员。 if (this.XHR.readyState == 1) { this.Result = "正在等待执行..."; } if (this.XHR.readyState == 4 || this.XHR.readyState == "complete") { debugger; alert("等于4啦"); if (this.XHR.status == 200) { if (this.Type == 'XML') { this.Result = this.XHR.responseXML; } else { this.Result = this.XHR.responseText; } } } }, Get: function (TagetURL, ResponseType) { this.URL = TagetURL + "&randcode=" + (new Date()).valueOf(); if (ResponseType != undefined && TagetURL != undefined) { this.Type = ResponseType.toUpperCase(); } if (window.XMLHttpRequest) { this.XHR.open("get", this.URL); this.XHR.onreadystatechange = Bind(this,this.ResponseHandle);//关键之处,问题源头在这里。通过这里引用this.ResponseHandle函数,注意这里不能写成this.ResponseHandle(),否则没效果,所以没有()就不能传送参数了。 this.XHR.send(null); } else { this.XHR.open("get", this.URL, true); this.XHR.onreadystatechange = this.ResponseHandle; this.XHR.send(); } } } var obj = new AjaxClass(); obj.Get("TreeHandler.ashx?id=0"); 年月日控件 jqgrid生成的链接点击显示不出fancybox效果 关于对象生成,原型链上的数据初始化问题 有谁知道怎么在条件里直接赋值并且判断 求救用javascript教研计算公式 请问IFRAME的滚动条颜色怎么改变? 请问,哪位高手能解决这个javascript问题?送分100!!!!!!!!!!!!!!!!!!!!!!! 关于<Script FOR="player" EVENT="initOK"> 这个语句,大家请看 一个关于提交的常见问题? Javascript 中怎么处理"或者' 问题 如何在不同的浏览器中加载 .jnlp 应用程序? 获取行的隐藏input的值
function myClass(){
this.a = "origin";
}myClass.prototype =
{
b:function(){
function d(obj){
alert(obj.a);
obj.a='new';
}
d(this);
alert(this.a);
}
}
var obj = new myClass();
obj.b();
this.a = "fuck you"; //这里定义成员a给下面的函数使用
}myClass.prototype =
{ b:function(){
var that = this;
function d(){
alert(that.a); //请问这里如果引用上面定义的变量a弹出fuckyou?同时还要在这里用this.a = "shit";达到修改上面的变量a的效果 }
d();
}
}
var obj = new myClass();
obj.b();
prototype还能这么写?学习了
尽量不要通过参数解决,因为我是通过类似句柄来引用函数,我贴上我问题的全部代码:function AjaxClass()
{
this.URL = "";
this.Type = '';
this.XHR = this.Create() ;
this.Result = '';}
AjaxClass.prototype =
{
Create:function(){
var xmlHttp = false;
if(window.XMLHttpRequest){ //在非IE中创建XMLHttpRequest对象
xmlHttp = new XMLHttpRequest();
}else if(window.ActiveXObject){
try{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); //高版本IE
}catch(error1){//创建IP高版本XMLHttpRequest对象失败
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); //低版本IE
}catch(error2){ //创建IP低版本XMLHttpRequest对象失败
xmlHttp = false;
}
}
}
if(!xmlHttp){
alert("创建XMLHttpRequest对象失败!程序无法运行,请检查您的浏览器类型与版本!");
return false;
}
return xmlHttp;
},
ResponseHandle:function(){
alert(this.XHR); //关键之处:问题出在这里,通过this引用的成员比如this.XHR都会显示undefined,我要的就是能正常引用上面的成员。
if(this.XHR.readyState == 1){this.Result = "正在等待执行...";}
if(this.XHR.readyState == 4 || this.XHR.readyState == "complete"){ alert("等于4啦");
if(this.XHR.status == 200){
if(this.Type == 'XML'){
this.Result = this.XHR.responseXML;
}else{
this.Result = this.XHR.responseText;
}
}
}
},
Get:function(TagetURL,ResponseType){
this.URL = TagetURL+"&randcode="+(new Date()).valueOf();
if(ResponseType != undefined && TagetURL != undefined){
this.Type = ResponseType.toUpperCase();
}
if(window.XMLHttpRequest){
this.XHR.open("get",this.URL);
this.XHR.onreadystatechange = this.ResponseHandle; //关键之处,问题源头在这里。通过这里引用this.ResponseHandle函数,注意这里不能写成this.ResponseHandle(),否则没效果,所以没有()就不能传送参数了。
this.XHR.send(null);
}else{
this.XHR.open("get", this.URL, true);
this.XHR.onreadystatechange = this.ResponseHandle;
this.XHR.send();
}
}
}var obj = new AjaxClass();
obj.Get("ajax.php?msg=fuckyou");
function Bind(obj,fun) {
return function () {
fun.apply(obj, [1, 2]);
//[1, 2]是传给ResponseHandle的参数,见过别人这样取参数的var args = Array.prototype.slice.call(arguments);
}
} function AjaxClass() {
this.URL = ""; this.Type = '';
this.XHR = this.Create();
this.Result = '';
}
AjaxClass.prototype =
{
Create: function () {
var xmlHttp = false;
if (window.XMLHttpRequest) {
//在非IE中创建XMLHttpRequest对象
xmlHttp = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
//高版本IE
} catch (error1) {//创建IP高版本XMLHttpRequest对象失败
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
//低版本IE
} catch (error2) { //创建IP低版本XMLHttpRequest对象失败
xmlHttp = false;
}
}
}
if (!xmlHttp) {
alert("创建XMLHttpRequest对象失败!程序无法运行,请检查您的浏览器类型与版本!");
return false;
}
return xmlHttp;
},
ResponseHandle: function () {
//alert(this.XHR); 关键之处:问题出在这里,通过this引用的成员比如this.XHR都会显示undefined,我要的就是能正常引用上面的成员。
if (this.XHR.readyState == 1) { this.Result = "正在等待执行..."; }
if (this.XHR.readyState == 4 || this.XHR.readyState == "complete") {
debugger;
alert("等于4啦");
if (this.XHR.status == 200) {
if (this.Type == 'XML') {
this.Result = this.XHR.responseXML;
}
else {
this.Result = this.XHR.responseText;
}
}
}
},
Get: function (TagetURL, ResponseType) {
this.URL = TagetURL + "&randcode=" + (new Date()).valueOf();
if (ResponseType != undefined && TagetURL != undefined) {
this.Type = ResponseType.toUpperCase();
}
if (window.XMLHttpRequest) {
this.XHR.open("get", this.URL);
this.XHR.onreadystatechange = Bind(this,this.ResponseHandle);//关键之处,问题源头在这里。通过这里引用this.ResponseHandle函数,注意这里不能写成this.ResponseHandle(),否则没效果,所以没有()就不能传送参数了。 this.XHR.send(null);
}
else {
this.XHR.open("get", this.URL, true);
this.XHR.onreadystatechange = this.ResponseHandle; this.XHR.send();
}
}
}
var obj = new AjaxClass();
obj.Get("TreeHandler.ashx?id=0");
return function () {
fun.apply(obj, [1, 2]);
//[1, 2]是传给ResponseHandle的参数,见过别人这样取参数的var args = Array.prototype.slice.call(arguments);
}
} function AjaxClass() {
this.URL = ""; this.Type = '';
this.XHR = this.Create();
this.Result = '';
}
AjaxClass.prototype =
{
Create: function () {
var xmlHttp = false;
if (window.XMLHttpRequest) {
//在非IE中创建XMLHttpRequest对象
xmlHttp = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
//高版本IE
} catch (error1) {//创建IP高版本XMLHttpRequest对象失败
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
//低版本IE
} catch (error2) { //创建IP低版本XMLHttpRequest对象失败
xmlHttp = false;
}
}
}
if (!xmlHttp) {
alert("创建XMLHttpRequest对象失败!程序无法运行,请检查您的浏览器类型与版本!");
return false;
}
return xmlHttp;
},
ResponseHandle: function () {
//alert(this.XHR); 关键之处:问题出在这里,通过this引用的成员比如this.XHR都会显示undefined,我要的就是能正常引用上面的成员。
if (this.XHR.readyState == 1) { this.Result = "正在等待执行..."; }
if (this.XHR.readyState == 4 || this.XHR.readyState == "complete") {
debugger;
alert("等于4啦");
if (this.XHR.status == 200) {
if (this.Type == 'XML') {
this.Result = this.XHR.responseXML;
}
else {
this.Result = this.XHR.responseText;
}
}
}
},
Get: function (TagetURL, ResponseType) {
this.URL = TagetURL + "&randcode=" + (new Date()).valueOf();
if (ResponseType != undefined && TagetURL != undefined) {
this.Type = ResponseType.toUpperCase();
}
if (window.XMLHttpRequest) {
this.XHR.open("get", this.URL);
this.XHR.onreadystatechange = Bind(this,this.ResponseHandle);//关键之处,问题源头在这里。通过这里引用this.ResponseHandle函数,注意这里不能写成this.ResponseHandle(),否则没效果,所以没有()就不能传送参数了。 this.XHR.send(null);
}
else {
this.XHR.open("get", this.URL, true);
this.XHR.onreadystatechange = this.ResponseHandle; this.XHR.send();
}
}
}
var obj = new AjaxClass();
obj.Get("TreeHandler.ashx?id=0");