<script language="javascript">
function oop(){
    var a=1;
this.fun1=function(){
fun2();
}
function fun2(){
fun3();
}
function fun3(){
alert(this.a);
}
}
var obj=new oop();
obj.a=5;
obj.fun1()
</script>
这个a为什么弹出来的是undefinde呢

解决方案 »

  1.   

     你没有理解a的生存周期!
    a在fun3中自然没有定义!也没有从oop中传递过来!
      

  2.   

    this  里面 没有a
    你的this指的不是oop()
      

  3.   

    把var a=1;写成全局变量的话。
    alert(a);就有值了。
      

  4.   

    this是谁调用就是谁 这里应该是func2
      

  5.   

    a是function oop(){}中的局部变量,在执行function fun3的时候已经超出作用域了
      

  6.   

    而且用this.a也不对的,this是window对象,没有a这个属性
      

  7.   

    你的这个js OOP思想压根就不对。根本不是JS的OOP
      

  8.   


    <script language="javascript">
        function oop() {
            var a = 1;
            this.fun1 = function () {
                fun2();
            }
            function fun2() {
                fun3();
            }
            function fun3() {
                alert(this.a);
            }
        }
        var obj = new oop();
        obj.a = 5;
        obj.fun1()    function OOP()
        {
            this.a = 1;
            this.func1 = function () {
                this.func2();
            }
            this.func2 = function () {
                this.func3();
            }
            this.func3 = function () {
                alert(this.a);
            };
        };    var o = new OOP();
        o.a = 5;
        o.func1();</script>你试下本地测试通过
      

  9.   

    function fun3(){
    alert(this.a);
    }this.fun1=function(){
    fun2();
    } 这两个this指代的不是同一个对象 
    对于
    function fun3(){
    alert(this.a);
    }
    如果fun3() 这样调用  this指向window
    如果new fun3() this指向当前对象 
    这样说 LZ能明白了么
      

  10.   


    function oop(){
      var a=1;
    this.fun1=function(){
    fun2(this.a);
    }
    function fun2(x){
    fun3(x);
    }
    function fun3(x){
    console.log(x);
    }
    }
    var obj=new oop();
    obj.a=5;
    obj.fun1()
      

  11.   

    将var a=1;
    改为
    this.a=1;
      

  12.   

    window.onload=show_move;//页面载入
    function show_move(){
    alert("a");
    var m=new move();
    m.auto_Speed=5000;
    m.Speed=0.01;
    m.Space=5;
    m.PageWidth=150;
    m.fill=0;
        m.MoveLock=false;
    m.Comp=0;
    m.start();
    }
    function move(){
    this.auto_Speed=2000;//自动滚动时的速度
    this.Speed = 5; //速度(毫秒){数值越大,速度越慢}
    this.Space = 5; //每次移动(px)
    this.PageWidth = 150; //翻页宽度
    this.fill = 0; //整体移位
    this.MoveLock = false;//判断是否要自动滚动
    this.MoveTimeObj;//按钮控制
    this.Comp = 0;
    this.AutoPlayObj;//自动

    this.start=function(){
    this.GetObj("List2").innerHTML = this.GetObj("List1").innerHTML;
    this.GetObj('ISL_Cont').scrollLeft = this.fill;
    this.GetObj("ISL_Cont").onmouseover = function(){
     clearInterval(this.AutoPlayObj);
    }
    this.GetObj("ISL_Cont").onmouseout = function(){
     this.AutoPlay();
    }

    this.AutoPlay();
    }
    this.GetObj=function(objName){
     if(document.getElementById){
    return eval('document.getElementById("'+objName+'")');
     }else{
    return eval('document.all.'+objName);
     }
    }

    this.AutoPlay=function(){ //自动滚动
    clearInterval(this.AutoPlayObj);
    this.AutoPlayObj = setInterval('this.ISL_GoDown();this.ISL_StopDown();',this.auto_Speed); //间隔时间
    }

    this.ISL_GoUp=function(){ //左翻开始
       if(this.MoveLock) return;
       clearInterval(this.AutoPlayObj);
       this.MoveLock = true;
       this.MoveTimeObj = setInterval('this.ISL_ScrUp();',this.Speed);
    }

    this.ISL_StopUp=function(){ //左翻停止
       clearInterval(this.MoveTimeObj);
       if(this.GetObj('ISL_Cont').scrollLeft % this.PageWidth - this.fill != 0){
      this.Comp = fill - (this.GetObj('ISL_Cont').scrollLeft % this.PageWidth);
      this.CompScr();
       }else{
      this.MoveLock = false;
       }
       this.AutoPlay();
    }

     this.ISL_ScrUp=function(){ //左翻动作
    if(this.GetObj('ISL_Cont').scrollLeft <= 0){
     this.GetObj('ISL_Cont').scrollLeft = this.GetObj('ISL_Cont').scrollLeft + this.GetObj('List1').offsetWidth;
    }
    this.GetObj('ISL_Cont').scrollLeft -= this.Space ;
    }

    this.ISL_GoDown=function(){ //右翻
    clearInterval(this.MoveTimeObj);
    if(this.MoveLock) return;
    clearInterval(this.AutoPlayObj);
    this.MoveLock = true;
    this.ISL_ScrDown();
    this.MoveTimeObj = setInterval('this.ISL_ScrDown()',this.Speed);
    }

    this.ISL_StopDown=function(){ //右翻停止
    clearInterval(this.MoveTimeObj);
    if(this.GetObj('ISL_Cont').scrollLeft % this.PageWidth - this.fill != 0 ){
       this.Comp = this.PageWidth - this.GetObj('ISL_Cont').scrollLeft % this.PageWidth + this.fill;
       this.CompScr();
    }else{
       this.MoveLock = false;
    }
    this.AutoPlay();
    }

    this.ISL_ScrDown=function(){ //右翻动作
    if(this.GetObj('ISL_Cont').scrollLeft >= this.GetObj('List1').scrollWidth){
    this.GetObj('ISL_Cont').scrollLeft = this.GetObj('ISL_Cont').scrollLeft - this.GetObj('List1').scrollWidth;
    }
    this.GetObj('ISL_Cont').scrollLeft += this.Space ;
    }

    this.CompScr=function(){
       var num;
       if(this.Comp == 0){
       this.MoveLock = false;return;
       }
       if(this.Comp < 0){ //左翻
      if(this.Comp < -this.Space){
      this.Comp += this.Space;
      num = this.Space;
      }else{
      num = -this.Comp;
      this.Comp = 0;
      }
      this.GetObj('ISL_Cont').scrollLeft -= num;
      setTimeout('this.CompScr()',this.Speed);
    }else{ //右翻
     if(this.Comp > this.Space){
    this.Comp -= this.Space;
    num = this.Space;
     }else{
    num = this.Comp;
    Comp = 0;
     }
    this.GetObj('ISL_Cont').scrollLeft += num;
    setTimeout('this.CompScr()',this.Speed);
    }
    }
    }这段代码为什么有好多都出现方法未定义呢
      

  13.   

    function OOP()
        {
            this.a = 1;
            this.func1 = function () {
                this.func2();
            }
            this.func2 = function () {
                this.func3();
            }
            this.func3 = function () {
                alert(this.a);
            };
        };    var o = new OOP();
        o.a = 5;
        o.func1();
      

  14.   

    正解。
    定义类型时this.*定义的是成员,分为成员变量与成员函数。
    var 定义的是变量,、根据其所在位置可分为局部变量与全局变量。
    function 定义的是函数,根据生存期由其定义位置确定。(函数内部定义的函数无法从函数外部访问)
    可以把内部定义的函数理解为private static,即外部不可见,但不能访问this。下面的例子说明了上面的各种定义:<script type="text/javascript">
    function Object1(){
    var a=1; //1
    this.fun1=function(op){
    if(op)
    this.fun2();
    else
    fun3();
    }
    this.fun2=function(){
    alert(a); //1:在函数体内,可访问1处的a,输出1
    alert(this.a); //2:是成员函数,可访问2处定义的成员变量a,输出5
    }
    function fun3() {
    alert(a); //1:在函数体内,可访问1处的a,输出1
    alert(this.a); //??:不是成员函数,不能访问成员变量,输出undefined
    }
    }var obj=new Object1();obj.a=5; //2
    obj.fun1(1);
    //obj.fun1(0);alert(obj.fun2); //有成员函数fun2,输出fun2的定义
    alert(obj.fun3); //无法访问函数体内定义的函数,且无名叫fun3的成员,故输出undefined
    </script>
      

  15.   


        this.fun1=function(op){
            if(op)
                this.fun2();
            else
                fun3();
        }op为1时执行this.fun2()
    op为0时执行fun3()js的标准是弱类型判断的,0为假,非0为真。