当前位置: 移动技术网 > IT编程>开发语言>JavaScript > javascript面向对象的写法(代码教程)

javascript面向对象的写法(代码教程)

2019年03月23日  | 移动技术网IT编程  | 我要评论

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>  

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网