//定 义一个动物类, 包含动物性别、动物年龄属性,以及一个说明基 本信息的方法;
//定 义一个动物类, 包含动物性别、动物年龄属性,以及一个说明基 本信息的方法;
//创建一条哈士奇,说明狗的基本信息并完成狗吠;
//创建一条哈士奇,说明狗的基本信息并完成狗吠;
//创建一条哈士奇,说明狗的基本信息并完成狗吠;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <script> // 定义动物类 function Anima(name,age,sang){ this.name=name; this.age = age; this.sang=sang; } Anima.prototype.index=function(){ document.write("姓名:"+this.name+"年龄"+this.age); document.write("我会----"+this.sang+'<br/>'); } // 定义狗 function Dog(name,age,sang){ // 只继承父类的属性 Anima.apply(this,[name,age,sang]); } // 继承父类的方法 Dog.prototype = new Anima(); Dog.prototype.say=function(){ document.write("我还会----跳舞"+'<br/>'); } var dog = new Dog('哈士奇',4,'嗷嗷叫'); dog.index(); dog.say(); document.write('<hr/>'); // 定义猫 function Cat(name,age,sang){ Anima.apply(this,[name,age,sang]); } Cat.prototype = new Anima(); // 猫特有的方法 Cat.prototype.say=function(){ document.write("我还会----多叫几声"+'<br/>'); } var cat = new Cat('波斯猫',2,'喵喵叫'); cat.index(); cat.say(); </script> </body> </html>
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051'运行结果:
姓名:哈士奇年龄4我会----嗷嗷叫
我还会----跳舞
——————————————————————————————————
姓名:波斯猫年龄2我会----喵喵叫
我还会----多叫几声