花边管理软件官方网站

JS也能实现构造函数

在上一篇,讲述了在JS中怎么实现普通类与静态类的书写方式,但却没有说到怎么实现带参数的构造函数,所以就有一下文章。
但我们需要创建一个对象,并传入参数时,看看JavaScript是怎么实现的。
<script language="javascript">    
function test()
{        
    this.initialize.apply(this, arguments);
}
test.prototype = {
initialize:function ()
 { 
  this.a = arguments[0];
  this.b = arguments[1];
 },
 geta:function ()
    {        
        return this.a;
    },
    getb:function ()
    {
        return this.b;
    }
}
   //看看这里的调用方式
   var test1 = new test('lgc','lhz');
 
 var a = test1.geta();  
          
    </script>
posted @ 2006-11-24 12:50  花边软件,花边管理软件,服装(鞋)管理软件  Views(217)  Comments(0)    收藏  举报
花边管理软件官方网站