JS中创建对象并用prototype属性动态扩展方法
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script type="text/javascript">
Student = function(sname,sno,sage){
this.sname=sname;
this.sno=sno;
this.sage=sage;
this.getSage = function(){
return this.sage;
}
} //定义学生类,有姓名,学号,年龄属性,以及获得年龄方法
var stu=new Student("czx",2018,22);
alert(stu.sname);
alert(stu.sno);
alert(stu.getSage());
Student.prototype.getSname= function(){
return this.sname;
} //使用Object超类的prototype属性对原本定义类属性及方法,进行动态扩展
alert("这是动态扩展的方法"+stu.getSname());
</script>
</body>
</html>