我的漫漫程序之旅

          專注于JavaWeb開發
          隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
          數據加載中……

          JavaScript面向對象基礎 (Object Oriented Basics)

          <HTML>
           
          <HEAD>
            
          <TITLE> Object oriented Basics</TITLE>
           
          </HEAD>
           
          <BODY>
          <script>
              
          // A simple function which takes a name and saves it to the current context
              function User(name)
              
          {
                  
          this.name = name;
              }

              
          //Create a new instance of that function ,with the specified name
              var me = new User("zdw");
              
          //We can see that its name has been set as a property of itself
              alert(me.name);
              
          //And that it is an instance of the User object
              alert(me.constructor == User);
              
          //Now,since User() is just a function,what happens 
              //when we treat it as such?
              User("admin");
              
          //Since its 'this' context wasn't set,it defaults to the global 'window'
              //object,meaning that window.name is equal to the name provided
              alert(window.name);
              
          //Create a new,simple,Person object
              function Person(name)
              
          {
                  
          this.name = name;
              }

              
          //Create a new Person object
              var p1 = new Person("person");
              
          //Alsos create a new Person Object(from the constructor reference of the first)
              var p2 = new p1.constructor("zhangsan");
              
          //show result
              alert(p2.name);
          </script>
           
          </BODY>
          </HTML>


          posted on 2008-07-18 16:45 々上善若水々 閱讀(216) 評論(0)  編輯  收藏 所屬分類: JavaScript

          主站蜘蛛池模板: 沙湾县| 蛟河市| 屏边| 桑日县| 出国| 蚌埠市| 会同县| 石景山区| 双城市| 蒲城县| 翼城县| 静乐县| 偏关县| 晋城| 图木舒克市| 阳信县| 郑州市| 东平县| 洞头县| 阳谷县| 茶陵县| 泰宁县| 东城区| 三台县| 邓州市| 晋城| 自贡市| 鄂托克前旗| 西乌珠穆沁旗| 阿克陶县| 堆龙德庆县| 休宁县| 石嘴山市| 高要市| 阿城市| 亳州市| 惠州市| 大渡口区| 腾冲县| 迭部县| 时尚|