zoukankan      html  css  js  c++  java
  • JavaScript设计模式-2高级类.

      1 <!DOCTYPE html>
      2 <html>
      3     <head>
      4         <meta charset="UTF-8">
      5         <title>javascript高级语法2-高级类</title>
      6     </head>
      7     <body>
      8         <script>
      9     /*        
     10             //在js中利用function来定义类。
     11             function Shape(){
     12                 var x=1;
     13                 var y=2;
     14             }
     15             //实例化对象;
     16             var a = new Shape();
     17             // 在类的内部用var定义的是私有变量。如何定义共有变量呢?
     18             function Shape2(){
     19                 this.x = 1;
     20                 this.y = 2;
     21             }
     22             var b = new Shape2();
     23             //this代表的是当前的实例。
     24             alert(a.x);  //undefined
     25             alert(b.x);  //1
     26             
     27             //除了定义私有变量外 还可以用var定义私有函数。
     28             function Shape3(){
     29                 var show=function(){
     30                     //私有函数
     31                 }
     32                 this.show2 = function(){
     33                     //公有函数
     34                 }
     35             }
     36             
     37             //用js模仿oop编程
     38              function Shape4(x,y){
     39                  var a = 0,b = 0;
     40                  //模仿oop编程的构造函数
     41                  var init = function(){
     42                      a = x;
     43                      b = y;
     44                  };
     45                  init();
     46                  
     47                  this.getX = function(){
     48                      return a;
     49                  }
     50              }
     51               var d = new Shape4(2,4);
     52               alert(d.getX());
     53               
     54               // js中的静态方法是作用到类身上的,而不是对象上
     55               function Person(){this.str = "zhangdan"}
     56               //静态变量
     57               Person.age = 10;
     58               Person.show = function(obj){
     59                   return obj.str    
     60               }
     61               alert(Person.show(new Person()));
     62               
     63             
     64              //简单类定义方法
     65              var a = {};
     66              var array = [];
     67              a["name"] = "zhangdan";
     68              alert(a.name);
     69              
     70    */            
     71                 //自定义map类
     72                 function jMap(){
     73                     //私有变量
     74                     var arr= {};
     75                     //增加
     76                     this.put = function(key,value){
     77                         arr[key] = value;
     78                     }
     79                     //查询
     80                     this.get = function(key){
     81                         if(arr[key]){
     82                             return arr[key]
     83                         }else{
     84                             return -1
     85                         }
     86                     }
     87                     //删除
     88                     this.remove = function(key){
     89                         delete arr[key]
     90                     }
     91                     //遍历
     92                     this.eachMap = function(fn){
     93                         for(var key in arr){
     94                             fn(key,arr[key]);
     95                         }
     96                     }
     97                 }
     98                 
     99                 var country =new jMap();
    100                 country.put("01","zg");
    101                 country.put("02","tg");
    102                 country.put("03","mg");
    103                 country.put("04","jnd");
    104                 
    105 //              alert(country.get("01"));
    106                 
    107                 country.eachMap(function(key,value){
    108                     document.write(key+"-->"+value+"<br/>");
    109                 })
    110         </script>
    111     </body>
    112 </html>
  • 相关阅读:
    《我是一只IT小小鸟》
    实现对字符串的反转输出与句子的反转输出
    7.13学习记录
    CentOS 7 不能连接网路的解决方法
    Xshell连接linux服务器不成功的乌龙问题
    Python基础(二)数据类型
    Python基础(一)
    UML精粹3
    UML精粹2
    UML精粹1
  • 原文地址:https://www.cnblogs.com/chengyunshen/p/7191732.html
Copyright © 2011-2022 走看看