posts - 59, comments - 244, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          個人平時常用的兩種js類寫法

          Posted on 2013-01-08 00:20 penngo 閱讀(2031) 評論(0)  編輯  收藏 所屬分類: javascript
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
            <html>
            <head>
              <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
              <script type="text/javascript">
              
          // 第一種
              var Test1 = function(){
                  
          var name = "";
                  
          this.setName = function(username){
                      name 
          = username;
                  }
                  
          this.getName = function(){
                      
          return name;
                  }
                  
          this.sayHello = function(){
                      
          return "Hello " + this.getName();
                  }
                  
          return this;
              }();
              Test1.setName(
          "penngo");
              console.log(
          "Test1======" + Test1.getName());
              console.log(
          "Test1======" + Test1.sayHello());

              
          // 第二種
              var Test2 = function(){
                  
          var name = "";
                  
          return {
                      setName:
          function(username){
                          name 
          = username;
                      },
                      getName:
          function(){
                          
          return name;
                      },
                      sayHello:
          function(){
                          
          return "Hello " + this.getName();
                      }
                  };
              }();
              Test2.setName(
          "penngo");
              console.log(
          "Test2======" + Test2.getName());
                  console.log(
          "Test2======" + Test2.sayHello());
              
          </script>
            </head>
            <body>
            </body>
            </html>
          主站蜘蛛池模板: 武强县| 扎囊县| 卓资县| 临沧市| 许昌县| 山阳县| 三明市| 河池市| 通辽市| 延寿县| 滨州市| 峨眉山市| 鄂州市| 岳阳市| 军事| 芮城县| 嵊泗县| 扶余县| 台南县| 巴楚县| 平凉市| 邢台县| 喜德县| 广昌县| 吕梁市| 潼南县| 怀宁县| 元谋县| 泸定县| 承德市| 金昌市| 台安县| 英山县| 同德县| 苍山县| 纳雍县| 正宁县| 麦盖提县| 三门县| 吉安市| 怀远县|