您的位置:首页 > 其它

寄生组合式继承

2017-03-26 00:47 337 查看
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>

</body>
<script type="text/javascript">
function Super(name) {
this.name=name;
this.color="red";
}
Super.prototype.sayName=function () {
alert(this.name);

}
function sub(name,age) {
Super.call(this,name)
this.age=age;
}
sub.prototype=new Super();
sub.prototype.constructor=sub;
sub.prototype.sayAge=function () {
alert(this.age)

}

var supTest=new Super("chengkai")//父函数的属性不受影响。
var subTest=new sub("kai",23);//子函数也可以调用父函数的函数,同时传递自己定义的属性。

//sub.prototype.sayAge();
//sub.prototype.sayName();
subTest.sayAge();
subTest.sayName();
supTest.sayName();
alert(supTest.color);

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