<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] >[success] # Javascript面向對象編程(三):非構造函數的繼承 這個系列的[第一部分](http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_encapsulation.html)介紹了"封裝",[第二部分](http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance.html)介紹了使用構造函數實現"繼承"。 今天是最后一個部分,介紹不使用構造函數實現"繼承"。 >[success] ## 一、什么是"非構造函數"的繼承? 比如,現在有一個對象,叫做"中國人"。 ~~~   var Chinese = {     nation:'中國'   }; ~~~ 還有一個對象,叫做"醫生"。 ~~~   var Doctor ={     career:'醫生'   } ~~~ 請問怎樣才能讓"醫生"去繼承"中國人",也就是說,我怎樣才能生成一個"中國醫生"的對象 ? 這里要注意,這兩個對象都是普通對象,不是構造函數,無法使用構造函數方法實現`繼承`。 >[success] ## 二、object()方法 json格式的發明人Douglas Crockford,提出了一個object()函數,可以做到這一點。 ~~~  function object(o) {     function F() {}     F.prototype = o;     return new F();   } ~~~ 這個object()函數,其實只做一件事,就是把子對象的prototype屬性,指向父對象,從而使得子對象與父對象連在一起,使用的時候,第一步先在父對象的基礎上,生成子對象: ~~~   var Doctor = object(Chinese); ~~~ 然后,再加上子對象本身的屬性: ~~~   Doctor.career = '醫生'; ~~~ 這時,子對象已經繼承了父對象的屬性了。 ~~~   alert(Doctor.nation); //中國 ~~~ <br/> >[success] ## 三、淺拷貝 除了使用"prototype鏈"以外,還有另一種思路:把父對象的屬性,全部拷貝給子對象,也能實現繼承。 下面這個函數,就是在做拷貝: ~~~   function extendCopy(p) {     var c = {};     for (var i in p) {       c[i] = p[i];     }     c.uber = p;     return c;   } ~~~ 使用的時候,這樣寫: ~~~   var Doctor = extendCopy(Chinese);   Doctor.career = '醫生';   alert(Doctor.nation); // 中國 ~~~ 但是,這樣的拷貝有一個問題。那就是,如果父對象的屬性等于數組或另一個對象,那么實際上,子對象獲得的只是一個內存地址,而不是真正拷貝,因此存在父對象被篡改的可能。 請看,現在給Chinese添加一個"出生地"屬性,它的值是一個數組。 ~~~   Chinese.birthPlaces = ['北京','上海','香港']; ~~~ 通過extendCopy()函數,Doctor繼承了Chinese。 ~~~  var Doctor = extendCopy(Chinese); ~~~ 然后,我們為Doctor的"出生地"添加一個城市: ~~~   Doctor.birthPlaces.push('廈門'); ~~~ 發生了什么事?Chinese的"出生地"也被改掉了! ~~~   alert(Doctor.birthPlaces); //北京, 上海, 香港, 廈門   alert(Chinese.birthPlaces); //北京, 上海, 香港, 廈門 ~~~ 所以,extendCopy()只是拷貝基本類型的數據,我們把這種拷貝叫做"淺拷貝"。這是早期jQuery實現繼承的方式。 >[success] ## 四、深拷貝 所謂"深拷貝",就是能夠實現真正意義上的數組和對象的拷貝。它的實現并不難,只要遞歸調用"淺拷貝"就行了。 ~~~ function deepCopy(p, c) { var c = c || {}; for (var i in p) { if (typeof p[i] === 'object') { c[i] = (p[i].constructor === Array) ? [] : {}; deepCopy(p[i], c[i]); } else { c[i] = p[i]; } } return c; } ~~~ 使用的時候這樣寫: ~~~   var Doctor = deepCopy(Chinese); ~~~ 現在,給父對象加一個屬性,值為數組。然后,在子對象上修改這個屬性: ~~~   Chinese.birthPlaces = \['北京','上海','香港'\];   Doctor.birthPlaces.push('廈門'); ~~~ 這時,父對象就不會受到影響了。 ~~~   alert(Doctor.birthPlaces); //北京, 上海, 香港, 廈門   alert(Chinese.birthPlaces); //北京, 上海, 香港 ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看