複製代碼代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
/*
* 組合模式: 構造函數模式+原型模式
這種方式是javascript中最通用的創建對象的方式
變量類型屬性:用構造函數傳遞
函數類型屬性:用原型模式聲明
*/
function Student(name,age){
this.name=name;
this.age=age;
}
Student.prototype.setName=function(name2){
this.name=name2;
};
Student.prototype.getName=function(){
return this.name;
};
var stu1=new Student("小胡",21);
alert(stu1.getName());
</script>
</head>
<body>
</body>
</html>