var tmp = new aa();
tmp.a2 = "bb1"
alert(tmp.a2)

解决方案 »

  1.   

    呵呵,谢谢各位,我刚想到了个笨方法,不知道是不是合理
    <script type="text/javascript">
    <!--
    var aa = function(){};aa.prototype = {
    a1 : "aa",
    a2 : "bb",
    a3 : "cc",
    a4 : "dd",
    a5 : "ee",
    ....
    };aa.prototype.init = function(o){
    for(var key in o){
    this[n] = o[n];
    }
    };var tmp = new aa();
    tmp.init({
    a2 : "bb1",
    a4 : "dd1"
    });
    //-->
    </script>
      

  2.   

    方法一:<script type="text/javascript">
    var aa=function(){};
    aa.prototype.init =function(){
      this.a1= "aa";
      this.a2= "bb";
      this.a3= "cc";
      this.a4= "dd";
      this.a5= "ee";
    }aa.prototype.setProperty=function(obj){
      for(var x in obj){  this[x]=obj[x];    }
    }
    var Leixian=new aa();
    Leixian.init();document.write('All initialized properties in the object Leixian :<br>')
    for(var x in Leixian){
      if(typeof Leixian[x]==='string'){    document.write("  "+Leixian[x]+"<br>");  }
    }var obj={
      a1:"111",
      a3:"333"
    }Leixian.setProperty(obj);
    var arrMethod=[]
    document.write('<br><br>Let\'s view all properties which just be altered by setProperty method in the object Leixian:<br>')
    for(var x in Leixian){
      if(typeof Leixian[x]==='string'){    document.write(Leixian[x]+"<br>");  }
      else{    arrMethod.push(Leixian[x]);  }
    }document.write('<br><br>Now,maybe you wonder to view the all methods in the object Leixian, see below:<br>')
    for(var x in arrMethod){  document.write("method"+(x*1+1)+":<br>"+arrMethod[x]+"<br><br>");}
    </script>
      

  3.   

    不加注释的:
    <script type="text/javascript">
    var aa=function(){};
    aa.prototype.init =function(){
      this.a1= "aa";
      this.a2= "bb";
      this.a3= "cc";
      this.a4= "dd";
      this.a5= "ee";
    }aa.prototype.setProperty=function(obj){
      for(var x in obj){  this[x]=obj[x];    }
    }
    //------------------------
    var Leixian=new aa();
    Leixian.init();
    var obj={
      a1:"111",
      a3:"333"
    }Leixian.setProperty(obj);</script>
      

  4.   

    方法二(高级点的)
    <script type="text/javascript">
    (function(){
    var
      window = this,/*加速指向窗口并允许更名*/
      undefined,/*加速指向窗口并允许更名*/
      _aa = window.aa,/*在被覆盖时映射aa*/
      _$ = window.$;          /*在被覆盖时映射aa*/
    aa =window.$= function (obj) {
      return new aa.fn.init(obj);
    };
     
    aa.fn = aa.prototype = {
      init: function (obj) {
        this[0] = obj;
        this.a1= "aa";
        this.a2= "bb";
        this.a3= "cc";
        this.a4= "dd";
        this.a5= "ee";
        return this;
      },
      setProperty:function(obj){
        for(var x in obj){  this[x]=obj[x];    }
      }
    }aa.fn.init.prototype = aa.fn;
    })()
    //aa等同于$,用哪个都可以
    alert("这是$对象:\n"+$)
    alert("这是aa对象:\n"+aa)
    var b=new $();
    var obj={
      a1:"111",
      a4:"444"
    }
    b.setProperty(obj);
    alert("没改变的属性a2:"+b.a2)
    alert("改变了的属性a4:"+b.a4)
    </script>
      

  5.   

    哇~~~!! ◎_◎楼上的你太牛了!第一种方法跟我想到的基本一样第二种方法我完全没见过唉,高手~~~~学习啦,哈哈!!想问一下,“aa.fn = aa.prototype = ”这连续连个等号是啥意思啊?另外,我想知道子类如何继承父类的参数比如<script type="text/javascript">
    <!--
    var aa = function(){};
    aa.prototype = {
    a : "aa",
    b : "bb"
    c : "cc"
    }aa.prototype.bb = function(){
    var btn = $("button");
    btn.onclick = function(){
    var tmp = new aa(); //这是我在网上看到的方法
    tmp.action(); //除了这种方法意外还有更好的办法吗?
    }
    }aa.prototype.action = function(){
    ...
    }
    //-->
    </script>