<!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>
<title>无标题页</title>
</head>
<body>
<script>
function Animal()
{
this.name="无名";//默认名称
this.shoutNum=3;
this.shout=null;
}
function Cat(name)
{
if(undefined!=name&&""!=name)//实现构造函数的重载
{
this.name=name;
}
this.shout=function(){ //Animal中的shout()的Cat实现
var result = "";
for (var i = 0; i < this.shoutNum; i++)
result += "喵 ";
return "我的名字叫" + this.name + " " + result;
}
}
Cat.prototype = new Animal(); //继承Animal
function Dog(name)
{
if(undefined!=name&&""!=name)
{
this.name=name;
}
this.shout=function(){
var result = "";
for (var i = 0; i < this.shoutNum; i++)
result += "汪 ";
return "我的名字叫" + this.name + " " + result;
}
}
Dog.prototype = new Animal();//继承Animal
var click={};
click.initcat=function()
{
var cat = new Cat()//调用无名name
//cat.shoutNum=5;//调用shoutNum=3;
return cat.shout();
};
click.cat=function()
{
var cat = new Cat("小咪");//传入名称小咪
cat.shoutNum=5;//设置叫声次数
return cat.shout();
};
click.dog=function()
{
var dog = new Dog("旺财");
dog.shoutNum=5;
return dog.shout();
};
alert(click.initcat());
alert(click.cat());
</script>
</body>
</html>