天天看点

javascript面向对象的写法

<script>
    //(面向对象的写法 DEMO)
    //---构造函数
    function CreateStudentRecord(name,age,telephone){
        //添加属性
        this.name=name;
        this.age=age;
        this.telephone=telephone;
    }
    //---添加方法
    CreateStudentRecord.prototype.showName=function (){
        alert('我的名字:'+this.name);
    }
    CreateStudentRecord.prototype.showAge=function (){
        alert('我的年龄:'+this.age);
    }
    CreateStudentRecord.prototype.showTelephone=function (){
        alert('我的手机号:'+this.telephone);
    }
    //类的实例化
    var student_a=new CreateStudentRecord('tom',28,'13103889999');
    //调用类中的方法
    student_a.showName();
    student_a.showAge();
    student_a.showTelephone();
    //实例化
    var student_b=new CreateStudentRecord('lili',26,'13103669888');
    student_b.showName();
</script>
           

继续阅读