<!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对象(八)
最新推荐文章于 2024-04-18 23:00:57 发布