我的漫漫程序之旅

          專注于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

          主站蜘蛛池模板: 绥宁县| 洛宁县| 衡南县| 庄河市| 绥宁县| 克什克腾旗| 若羌县| 新野县| 黄平县| 察哈| 安塞县| 达日县| 读书| 石狮市| 佳木斯市| 太湖县| 方山县| 固原市| 昭通市| 自治县| 佛冈县| 巩义市| 临海市| 武功县| 政和县| 三原县| 樟树市| 庆阳市| 交城县| 西盟| 额尔古纳市| 蒲江县| 平遥县| 临沧市| 华容县| 东丽区| 大港区| 石楼县| 抚远县| 朝阳县| 天全县|