这样可以吗
来源:7-2 编程练习
柒壹柒
2019-10-02 22:06:48
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
//补充代码
function Animal(name,food) {
this.name=name;
this.food=food;
this.say=function(){
alert(this.name+"喜欢吃"+this.food);
}
}
function Dog(){
this.name="狗狗";
this.food="骨头";
}
var aniDog=new Dog();
var ani= new Animal();
ani.say.call(aniDog,"","");
</script>
</body>
</html>1回答
同学你好, 这样写也是可以实现效果的。
建议优化:可以在定义Dog函数的内部通过call方法继承Animal的方法哦,然后通过给Dog传入参数的方式设置name和food值,让代码更加灵活哦

如果帮助到了你, 欢迎采纳!
祝学习愉快~~~
相似问题