您的位置:首页 > 其它

继承案例

2016-03-15 23:17 357 查看
<span style="font-size:18px;"><!DOCTYPE html>
<html>
<head>
<title> new document </title>
<meta charset="utf-8" />
<script>
//飞行物的构造函数
function Flyer(fname,speed){
//this->a380
this.fname=fname;
this.speed=speed;
}
Flyer.prototype.fly=function(){
console.log(this.fname+" 以时速 " +this.speed+" 飞行");
}
//飞机 是 飞行物
function Plane(fname,speed,capacity){
//this->a380
//Flyer.call(this,fname,speed);//借用父类型构造函数
Flyer.apply(this,arguments);
//fname和speed自有属性
this.capacity=capacity;
}
//设置继承: Plane.prototype继承Flyer.prototype
Object.setPrototypeOf(
Plane.prototype,Flyer.prototype
);
var bird=new Flyer("小麻雀",50);
//bird.__proto__=Flyer.prototype
//console.log(bird);
bird.fly();
var a380=new Plane("A380",1000,555);
//a380.__proto__=Plane.prototype
//console.log(a380);
a380.fly();
</script>
</head>

<body>

</body>
</html>
</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: