<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript">
function Student(){
//下面abcd就是屬性
//this,指代的當前對象,this.a = ..就是將"舉世無雙"賦值給這個對象的a屬性,
// 如果a屬性不存在,則賦給這個對象添加一個a屬性,再賦值
this.a = "舉世無雙";
this.b = "一騎當千";
this.c = "萬夫莫敵";
this.d = "所向披靡"
//下面的laugh和eat就是方法
this.laugh = function(){
document.getElementById("p1").innerHTML = "笑他";
}
this.eat = function(){
alert("少吃點吧,該減肥了");
}
function song(){
alert("一展歌喉")
}
song();
}
var stu = new Student();
//使用prototype關鍵字擴展一個函數
Student.prototype.goSchool = function(){
alert("去上學");
}
//創建了第二個Student對象
var stu2 = new Student();
function YYY(){
this.y = "我是小y";
}
Student.prototype = new YYY();
var stu3 = new Student;
</script>
</head>
<body>
<input type="button" value="a" onclick="alert(stu.a)">
<input type="button" value="b" onclick="alert(stu.b)">
<input type="button" value="c" onclick="alert(stu.c)">
<input type="button" value="d" onclick="alert(stu.d)">
<input type="button" value="laugh" onclick="stu.laugh()">
<input type="button" value="eat" onclick="stu.eat()">
<input type="button" value="song" onclick="Student">
<input type="button" value="擴展去上學" onclick="stu2.goSchool()">
<input type="button" value="繼承" onclick="alert(stu3.y)">
<p id="p1"></p>
</body>
</html>
javaScript系列之使用構造方法創建js對象(八)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.