运用JAVASCRIPT,写一个类,类名:student,他的属性:name,age,tall,他的方法:getName,getAge,getTall


前几天,看到网上的面试题,自己平时也用js,可一看,傻眼了,用js写类,想想这是很基础的东西,赶紧学习了几天,现在写一下,不对了还请大家指教:
题目:运用JAVASCRIPT,写一个类,类名:student,他的属性:name,age,tall,他的方法:getName,getAge,getTall

//首先定义函数
 function student_getName()
    {
        return this.name;
    }
    function student_getAge()
    {
        return this.age;
    }
    function student_getTall()
    {
        return this.tall;
    }
    //定义构造函数
    function student(n,a,t)
    {
        //初始化对象的属性
        this.name=n;
        this.age=a;
        this.tall=t;
        //定义对象的方法
        this.getName=student_getName;
        this.getAge=student_getAge;
        this.getTall=student_getTall;
    }
   function test()
   {
    //首先定义函数
   var s=new student("张三",25,175);
    alert(s.getName());
    alert(s.getAge());
    alert(s.getTall());
    }
posted @ 2007-01-11 13:26  黑星  阅读(3045)  评论(5编辑  收藏  举报