herodby
          技術 筆記
          posts - 14,  comments - 14,  trackbacks - 0

          ??
          ?? javascript 是一種基于原型的語言,沒有顯示的繼承符號,它有2種隱式的方式可以實現面向對象中的繼承:
          ???1 .???在子類中顯示的調用父類的構造函數.
          ??????????例子:
          ??????????????function? parent ()
          ?????????????{
          ????????????????????this.first = parentFirst;
          ????????????????????this.second = parentSecond;?
          ??????????????}
          ????????????? function child()
          ??????????????{
          ?????????????????????this.inherrit = parent;
          ?????????????????????this.inherrit();
          ?????????????????????this.second = childSecond;
          ????????????}
          ????????????? function parentFirst()
          ???????????{
          ??????????????????alert(" parent First method call !");
          ????????????}
          ???????????function parentSecond()
          ????????????{
          ??????????????????alert(" parent second method call !");
          ????????????}
          ??????????? function childFirst()
          ????????????{
          ??????????????????alert("child first method call !");
          ????????????}
          ????????????function childSecond()
          ????????????{
          ??????????????????alert("child second method call !");
          ????????????}
          ????????????function? printClass()
          ????????????{
          ??????????????????var?newObj = new child();
          ????????????????? newObj.first();
          ??????????????????newObj.second();
          ????????????}
          ????????????printClass();
          ?????????????發現最后打印出來結果是 parent first method call !?? child second method????call !
          ??????????我們可以看到,first 和second 方法最先在parent類中定義,在child類中 second方法又被重載了.child類的構造函數的前2行的作用就是做了一個簡單的原始繼承操作.
          先把parent的原型賦給child的inherrit方法,執行完parent的構造函數后parent的屬性就全在child的屬性列表里了.因為child中是用this來調用parent的構造函數的,javascript解釋器會把parent中的this,child中的this 理解成一個作用域下的this,所以就產生了繼承效果.
          ? 2.???繼承的第二種方式:? prototype方式.
          ??????? 把父類對象賦給 子類的prototype屬性.
          ??????? 把我們的子類改一下:
          ????????function child()
          ????????{
          ?????????????????????this.second = childSecond;
          ?????????}
          ?????????加入?? child.prototype = new?parent;這一行語句,執行,發現和第一種方式得到同樣的結果.
          ??????? 用prototype 方式繼承比調用構造函數方式更靈活.用prototype方式可以實現動態繼承,比如你用prototype方式給父類動態增加一個屬性或者方法,那么子類自動就會獲得這個屬性或者方法.??
          ???????????在javascript中你可以隨意的給對象增加屬性或者方法,但是當你給一個實例化的對象增加屬性的時候,這個增加的屬性只屬于這個實例,例如:
          ?? var newClass = new subClass();
          ? newClass.addprop = "新增屬性";
          如果想為該類的所有實例都增加一個屬性或者方法,要用prototype,? 例子
          ?? var newClass = new child();
          ? child.prototype.addprop = "新增屬性";
          ? 這樣newClass里就會有addprop這個屬性了.
          這也是我們給javascript內部對象增加屬性或者方法的方式,比如 String.prototype.toString={return "aaa";},不過javascript的內部對象的prototype屬性都是只讀的,所以只能增加內部對象的屬性,不能修改.
          ????? 突然感覺倒?prototype,用處原來如此之多,上邊寫的不過是其中一小步分啊.


          ??

          posted on 2006-08-09 17:30 鄧兵野 閱讀(1353) 評論(1)  編輯  收藏 所屬分類: javascript

          FeedBack:
          # re: javascript 基于原型的知識--如何實現繼承
          2008-09-28 16:53 | hellen
          如果把代碼貼全就更明了了  回復  更多評論
            

          <2008年9月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 建水县| 宜章县| 尖扎县| 新乡市| 夏河县| 万宁市| 花莲市| 尤溪县| 孝感市| 永城市| 新兴县| 荣昌县| 札达县| 凤庆县| 清远市| 鹤峰县| 麻城市| 睢宁县| 奎屯市| 黄陵县| 化州市| 苍南县| 盐池县| 修文县| 龙井市| 丰城市| 柞水县| 都匀市| 逊克县| 桂林市| 曲阳县| 蒙阴县| 库尔勒市| 揭阳市| 灯塔市| 湖州市| 利川市| 罗平县| 沁源县| 东海县| 柳河县|