在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>