您的位置:首页 > Web前端 > JavaScript

javascript 中实现继承及多态的例子

2010-04-01 15:11 501 查看
在js 中通过原型继承,实现了c++中虚函数的多态效果:

<html>
<head>
<title>test</title>
<script type="text/javascript">
function A() {
this.fun1 = function() {
alert("A");
}
this.fun2 = function() {
this.fun1();
}
}

function B() {
this.fun1 = function() {
alert("B");
}
}
B.prototype = new A();
</script>
</head>
<body>
<script type="text/javascript">
var a = new A();
a.fun1();
a.fun2();
var b = new B();
b.fun1();
b.fun2();
</script>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: