关于javascript引用父级变量的问题

【烂泥扶不上墙】 2012-01-30 07:43:49

function myClass(){
this.a = "fuck you"; //这里定义成员a给下面的函数使用
}

myClass.prototype =
{

b:function(){
function d(){
alert(this.a); //请问这里如果引用上面定义的变量a弹出fuckyou?同时还要在这里用this.a = "shit";达到修改上面的变量a的效果

}
d();
}
}
var obj = new myClass();
obj.b();
...全文
163 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
lhprince1 2012-02-03
  • 打赏
  • 举报
回复
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");
lhprince1 2012-02-03
  • 打赏
  • 举报
回复

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");
不耐烦 2012-01-30
  • 打赏
  • 举报
回复
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();
btga190 2012-01-30
  • 打赏
  • 举报
回复
共同解决
lhprince1 2012-01-30
  • 打赏
  • 举报
回复

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();
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lhprince1 的回复:]
JScript code


function myClass(){
this.a = "origin";
}

myClass.prototype =
{
b:function(){
function d(obj){
alert(obj.a);
obj.a='new';
……
[/Quote]

尽量不要通过参数解决,因为我是通过类似句柄来引用函数,我贴上我问题的全部代码:

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");
tjxray 2012-01-30
  • 打赏
  • 举报
回复
obj.prototype={func_name:function(){/...}}
prototype还能这么写?学习了

87,990

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧