function Obj(){
var a = "a";
var b = "b";
var c = "c";
function getA(){
return a;
}
function getB(){
return b;
}
function getC(){
return c;
}
function setA(a){
thsi.a = a;
}
function getB(b){
this.b = b;
}
function getC(c){
this.c = c;
}

}var o = new Obj();
document.write("o="+o);
HTML 输出结果: o=[object Object]document.write("o="+o.getA()); //报错!我要如何能得到Obj这个函数里面的方法???

解决方案 »

  1.   

    你这里定义的getA,getB...等函数都是Obj类的私有方法,是不能通过类的实例化去调用的
    你可以通过定义特权函数:
    this.getA = function(){return a};
    也可以在Obj类的外部给Obj原型添加getA 的方法:
    Obj.prototype.getA = function(){return a};
    这两种方法通过Obj的实例调用getA方法
      

  2.   

    function obj(a){
        this.a=a;
    }
    obj.prototype.getA = function(){
    alert(this.a);
    }
    var o = new obj("hello");
    o.getA();
      

  3.   

    function Obj(){
    this.a = "a";
    this.b = "b";
    this.c = "c";
    function getA(){
    return this.a;
    }
    function getB(){
    return this.b;
    }
    function getC(){
    return this.c;
    }
    function setA(a){
    thsi.a = a;
    }
    function getB(b){
    this.b = b;
    }
    function getC(c){
    this.c = c;
    }}
      

  4.   


    function Obj(){
    this.a = "";
    this.b = "";
    this.c = "";
    }

    Obj.prototype.setA = function(str){
    this.a = str;
    }

    Obj.prototype.getA = function(){
    return this.a;
    }

    Obj.prototype.getB = function(){
    return this.b;
    }

    Obj.prototype.getC = function(){
    return this.c;
    }

    var obj = new Obj()

    obj.setA("test");
    alert(obj.getA())
      

  5.   

    function Obj(){
            this.a = "";
            this.b = "";
            this.c = "";
        }
        
        Obj.prototype.setA = function(str){
            this.a = str;
        }
        
        Obj.prototype.getA = function(){
            return this.a;
        }
        
        Obj.prototype.getB = function(){
            return this.b;
        }
        
        Obj.prototype.getC = function(){
            return this.c;
        }
        
        var obj = new Obj()
        
        obj.setA("test");
        alert(obj.getA())
    up
      

  6.   

    牛人啊
    居然把用java的方法用在Javascript上
      

  7.   

    涉及到对象的私有属性,不可以直接调用的,应该简接的,像这样:function Obj(){
    var a = "a";
    var b = "b";
    var c = "c";
    function getA(){
    return a;
    }
    this.GetA=getA();
    function getB(){
    return b;
    }
    function getC(){
    return c;
    }
    function setA(a){
    this.a = a;
    }
    function getB(b){
    this.b = b;
    }
    function getC(c){
    this.c = c;
    }}var o = new Obj();
    console.log("o="+o.GetA); 
      

  8.   

    function Obj(){ 
    var a = "a"; 
    var b = "b"; 
    var c = "c"; 
    this.getA = function(){ 
    return a; 

    this.getB = function(){ 
    return b; 

    this.getC = function(){ 
    return c; 

    function setA(val){ 
    a = val;

    function getB(val){ 
    b = val;

    function getC(val){ 
    c = val;


    var o = new Obj(); 
    document.write("o="+o.getA()); 这是你要的效果吧,a,b,c只能通过set来传值
      

  9.   

    function Obj(){ 
    var a = "a"; 
    var b = "b"; 
    var c = "c"; 
    this.getA = function(){ 
    return a; 

    this.getB = function(){ 
    return b; 

    this.getC = function(){ 
    return c; 

    this.setA = function(val){ 
    a = val;

    this.setB = function(val){ 
    b = val;

    this.setC = function(val){ 
    c = val;


    var o = new Obj(); 
    document.write("o="+o.getA()); 这是你要的效果吧,a,b,c只能通过set来传值,上面的写错了