function auction(){
  this.price;
}auction.prototype.showInfo = function(){
  this.price = '20';
}function test(){
  var p = new auction();
  alert(p.price);
}
结果是null,可是我想得到的值是20,也就是showInfo方法中定义的值,请问我该如何取?ps:不要告诉我把showInfo方法也改为 function showInfo(){},因为showInfo中用到很多this定义的变量,一改就全乱套,所以请问还有其他什么方法谢谢。

解决方案 »

  1.   

        function auction(price){ 
          this.price=price; 
        } 
        auction.prototype.showInfo = function()
        { 
          //this.price = '20'; 
        } 
        function test()
        { 
          var p = new auction('20'); 
          alert(p.price);
        }这样呢?
      

  2.   

    TO qiheia:
      你理解错我的意思了,全局变量this.price在function auction中复制的话,alert有值  这个毋庸置疑,可是程序需要 this.price 在 auction.prototype.showInfo 中重新赋值了  我要拿的是 auction.prototype.showInfo 中的值,可是却是 undefined
      

  3.   

    要调用函数啊
    function test(){
      var p = new auction();
      p.showInfo();//那个是函数,你没有调用嘛,price就不可能是20了
      alert(p.price);
    }
      

  4.   

    怎么感觉跟我写的方式很不一样啊
       页面js一般用于赋值跟取值
    赋值:var定义一个值temp
          document.getElementById("属性的id").value=temp;
    取值与赋值差不多
      

  5.   

    function auction(){
      this.price;
      
    }auction.prototype.showInfo = function(){
      this.price = '20';
    return price;
    }function test(){
      var p = new auction();
      alert(p.price);
    }