您的位置:首页 > Web前端 > JavaScript

javascript面向对象的写法

2018-02-07 10:17 351 查看
<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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: