通过原型实现。这里边好像还有性能问题。
<html>
<body>
<script type="text/javascript">
function test(){
this.add = function(a, b){
return a + b;
}
}
var t = new test();
document.write(t.add(3, 4));
function test1(){
}
test1.prototype.add = function(a, b){
document.write("public method<br>");
return a + b;
}
var t1 = new test1();
document.write(t1.add(2, 3));
function test2(){
}
test2.add = function(a, b){
document.write("private method<br>");
return a + b;
}
document.write(test2.add(1, 2));
</script>
</body>
</html>