码迷,mamicode.com
首页 > 其他好文 > 详细

函数的继承

时间:2015-07-20 01:02:13      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:


关于函数的继承,子类继承父类属性

示例如下:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
</body>
<script>
function Animal(name,age){//最大父元素:动物
this.name=name;//所拥有的两个特征name和age
this.age=age;
this.show=function(){
console.log("动物的名字叫:"+this.name+" 动物的年龄为:"+this.age);
}
}

function Dog(name,age,lei){//动物的子元素Dog
//对象冒充法1
//this.inhert = Animal;
//this.inhert(name,age);
//delete this.inhert;

//对象冒充法2
//Animal.call(this,name,age);

//对象冒充法3
//Animal.apply(this,[name,age]);

//以上三种冒充法和下面真正继承法继承了动物的name和age特征
继承了动物的name和age
this.name = name;
this.age = age;

this.lei=lei;//同时添加lei的新特征
}
Dog.prototype = new Animal();//真正继承

function Cat(name,age,color){//动物的子元素Cat同时是jiafei和jiqimao的父元素
Animal.call(this,name,age);//继承了动物的name和age
this.color=color;//同时添加color的新特征
}


function bomei(name,age,lei,tixing){
Dog.call(this,name,age,lei);
this.tixing=tixing
}
var bomei = new bomei("博美",5,"犬类","小");
bomei.show();

function hashiqi(name,age,lei,tixing){
Dog.call(this,name,age);
this.tixing=tixing

}
var hashiqi = new hashiqi("哈士奇",2,"犬类","大");
hashiqi.show();


function jiafei(name,age,color,food){
Cat.call(this,name,age);
this.food=food
}
var jafei=new jiafei("小家",3,"黄色","鱼")
jafei.show();



function jiqimao(name,age,color,gongneng){//继承了Cat的所有特征
Cat.call(this,name,age,color);
this.gongneng=gongneng;//添加新特征gongneng
this.show1=function(){//建立函数
console.log("动物的名字叫:"+this.name+" 动物的年龄为:"+this.age+"颜色:"+this.color+"功能:"+this.gongneng);
}
}
//产生一个jiqimao的实例
var jiqimao=new jiqimao ("多拉A梦",58,"蓝白","空间袋")
jiqimao.show1();//调用show1()的函数
</script>
</html>

函数的继承

标签:

原文地址:http://www.cnblogs.com/zengjie123/p/4660229.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!