一个简单的javascript类定义例子
涵盖了javascript公有成员定义、私有成员定义、特权方法定义的简单示例
Java代码
- <script>
-
- function JsClass(privateParam,publicParam){
- var priMember = privateParam;
- this.pubMember = publicParam;
-
- function priMethod(){
- return "priMethod()";
- }
-
-
- this.privilegedMethod = function(){
- var str = "这是特权方法,我调用了\n";
- str += " 私有变量:" + priMember +"\n";
- str += " 私有方法:" + priMethod() +"\n";
- str += " 公共变量:" + this.pubMember +"\n";
- str += " 公共方法:" + this.pubMethod();
-
- return str;
- }
- }
-
-
- JsClass.prototype.pubMethod = function(){
- return "pubMethod()";
- }
-
-
- JsObject = new JsClass("priMember","pubMember");
-
-
-
-
-
- alert(JsObject.privilegedMethod());
- </script>