面向对象 继承~

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
function Person(name, sex)
{
this.name=name;
this.sex=sex;
}

Person.prototype.showName=function ()
{
alert(this.name);
};

Person.prototype.showSex=function ()
{
alert(this.sex);
};

//-------------------------------------

function Worker(name, sex, job)
{
//this->new出来的Worker对象
//构造函数伪装 调用父级的构造函数——为了继承属性
Person.call(this, name, sex);

this.job=job;
}

//原型链 通过原型来继承父级的方法
//Worker.prototype=Person.prototype;

for(var i in Person.prototype)
{
Worker.prototype[i]=Person.prototype[i];
}

Worker.prototype.showJob=function ()
{
alert(this.job);
};

var oP=new Person('zgz', '男');
var oW=new Worker('zgz', '男', '打杂的');

oP.showName();
oP.showSex();

oW.showName();
oW.showSex();
oW.showJob();
</script>
</head>

<body>
</body>
</html>

posted @ 2015-02-06 22:08  菜园子丶  阅读(132)  评论(0编辑  收藏  举报