本帖最后由 joulang 于 2012-01-30 07:48:13 编辑

解决方案 »

  1.   


    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();
      

  2.   

    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();
      

  3.   

    obj.prototype={func_name:function(){/...}}
    prototype还能这么写?学习了
      

  4.   


    尽量不要通过参数解决,因为我是通过类似句柄来引用函数,我贴上我问题的全部代码: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");
      

  5.   


            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");
      

  6.   

            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");