一個(gè)簡(jiǎn)單的javascript類定義例子
涵蓋了javascript公有成員定義、私有成員定義、特權(quán)方法定義的簡(jiǎn)單示例
Java代碼
- <script>
-
- function JsClass(privateParam,publicParam){
- var priMember = privateParam;
- this.pubMember = publicParam;
-
- function priMethod(){
- return "priMethod()";
- }
-
-
- this.privilegedMethod = function(){
- var str = "這是特權(quán)方法,我調(diào)用了\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>