Alex刺客

          Dancing fingers, damage world. -- 舞動手指,破壞世界.

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            57 隨筆 :: 0 文章 :: 76 評論 :: 0 Trackbacks
           1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
           2<html xmlns="http://www.w3.org/1999/xhtml">
           3    <head>
           4        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
           5        <title>原型鏈方式</title>
           6        <script type="text/javascript">
           7            /*
           8            *    項目: book -> Javascript高級程序設計.pdf -> 第四章 -> 4.2.1 繼承的方式
           9            *    說明:使用prototype屬性
          10            *    練習者: Alex刺客
          11            *    日期: 2009-12-13
          12            */

          13            
          14            /*
          15                原型鏈方式
          16                原型鏈的神奇之處在于突出顯示的代碼,這里把ClassB的prototype屬性設置成ClassA的實例。
          17                這很有意義,因為想要ClassA的所有屬性和方法。所以把ClassB的全部屬性設置成ClassA的實例。
          18                因為這種繼承方式使用了prototype屬性,所以instanceof運算符可以正確運行。
          19            */

          20            function ClassA () {}
          21            
          22            ClassA.prototype.color = 'red';
          23            ClassA.prototype.sayColor = function () {
          24                alert(this.color);
          25            }

          26            
          27            function ClassB () {}
          28            ClassB.prototype = new ClassA();
          29            //添加新方法
          30            ClassB.prototype.name = "ClassB";
          31            ClassB.prototype.sayName = function () {
          32                alert(this.name);
          33            }

          34            
          35            /*
          36                混合方式 對象冒充+原型鏈
          37                跟建造類一樣的問題也出現在繼承當中,所以也就產生了這種方式。
          38                用對象冒充繼承構造函數,用原型鏈繼承prototype對象的方法。
          39            */

          40            
          41            function ClassD ( sColor) {
          42                this.color = sColor;
          43                if(typeof ClassD._initMethod == "undefined"{
          44                    ClassD.prototype.sayColor = function () {
          45                        alert(this.color);
          46                    }

          47                    alert('ClassD我只生成一次!');
          48                    ClassD._initMethod = true;
          49                }

          50            }

          51            var cd = new ClassD();
          52            function ClassE (sColor, sName) {
          53                ClassD.call(this,sColor);
          54                this.name = sName;
          55                if(typeof ClassE._initMethod == "undefined"{
          56                    ClassE.prototype.sayName =function () {
          57                        alert(this.name);
          58                    }

          59                    alert('ClassE我只生成一次!');
          60                    ClassE._initMethod = true;
          61                }

          62            }

          63            ClassE.prototype = new ClassD();
          64            /*
          65                繼承機制不能采用動態化的原因是,prototype對象的唯一性。如果放入 if 區域 
          66                在代碼運行前,對象已被實例化了,并與原始的prototype對象聯系在一起。雖然用極
          67                晚綁定可使對原型對象的修改正確地返映出來,但是替換prototype對象卻不會對該對象
          68                產生任何影響。只有未來的對象實例才會反映出這種改變,這就使第一個實例變得不正確。
          69                
          70            */

          71            
          72            var ce1 = new ClassE("red","blueBoy");
          73            var ce2 = new ClassE("blue","redBoy");
          74            ce1.sayColor();
          75            ce1.sayName();
          76            ce2.sayColor();
          77            ce2.sayName();
          78            
          79            
          80        </script>
          81    </head>
          82    <body>
          83    </body>
          84</html>
          posted on 2009-12-13 23:11 Alex刺客 閱讀(296) 評論(0)  編輯  收藏 所屬分類: JavaScript
          主站蜘蛛池模板: 辉南县| 鸡泽县| 百色市| 宜宾县| 通州区| 抚顺县| 潢川县| 高雄市| 集贤县| 盐亭县| 秭归县| 德庆县| 婺源县| 江源县| 容城县| 神木县| 深圳市| 云安县| 德州市| 莱州市| 太仓市| 日喀则市| 靖西县| 龙南县| 荔波县| 怀仁县| 美姑县| 霍林郭勒市| 永顺县| 布尔津县| 沂源县| 米脂县| 涞源县| 南平市| 常德市| 黔东| 舒兰市| 贵港市| 中卫市| 金塔县| 隆德县|