<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>面向对象编程</title>
<meta charset="utf-8" />
</head>
<body>
<script>
var person = {
name: "张三",
age: 18,
getName: function () {
return this.name;
}
}
</script>
<script>
function parent() {
this.name = "张三";
this.show1 = function () {
alert("我是" + this.name);
}
}
function child() {
this.age = 18;
this.show2 = function () {
alert("我的年龄是"+this.age)
}
}
child.prototype = new parent();//child继承parent
var test = new child();
test.name = "李四";//继承父对象的name属性,并修改属性值
test.show1();//继承父对象的show1()方法
test.show2();//调用子对象的show2()方法
</script>
</body>
</html>