JavaScript不使用prototype和new實現繼承機制_javascript技巧

來源:互聯網
上載者:User

此方法並非筆者原創,筆者只是在前輩的基礎上,加以總結,得出一種簡潔實用的JavaScript繼承方法。

      傳統的JavaScript繼承基於prototype原型鏈,並且需要使用大量的new操作,代碼不夠簡潔,可讀性也不是很強,貌似還容易受到原型鏈汙染。

      筆者總結的繼承方式,簡潔明了,雖然不是最好的方式,但希望能給讀者帶來啟發。

      好了,廢話不多說,直接看代碼,注釋詳盡,一看就懂~~~

複製代碼 代碼如下:

 /**
  * Created by 楊元 on 14-11-11.
  * 不使用prototype實現繼承
  *
  */
 /**
  * Javascript對象複製,僅複製一層,且僅複製function屬性,不通用!
  * @param obj  要複製的對象
  * @returns  Object
  */
 Object.prototype.clone = function(){
     var _s = this,
         newObj = {};
     _s.each(function(key, value){
         if(Object.prototype.toString.call(value) === "[object Function]"){
             newObj[key] = value;
         }
     });
     return newObj;
 };
 /**
  * 遍曆obj所有自身屬性
  *
  * @param callback 回呼函數。回調時會包含兩個參數: key 屬性名稱,value 屬性值
  */
 Object.prototype.each = function(callback){
     var key = "",
         _this = this;
     for (key in _this){
         if(Object.prototype.hasOwnProperty.call(_this, key)){
             callback(key, _this[key]);
         }
     }
 };
 /**
  * 建立子類
  * @param ext obj,包含需要重寫或擴充的方法。
  * @returns Object
  */
 Object.prototype.extend = function(ext){
     var child = this.clone();
     ext.each(function(key, value){
         child[key] = value;
     });
     return child;
 };
 /**
  * 建立對象(執行個體)
  * @param arguments 可接受任意數量參數,作為構造器參數列表
  * @returns Object
  */
 Object.prototype.create = function(){
     var obj = this.clone();
     if(obj.construct){
         obj.construct.apply(obj, arguments);
     }
     return obj;
 };
 /**
  * Useage Example
  * 使用此種方式繼承,避免了繁瑣的prototype和new。
  * 但是目前筆者寫的這段樣本,只能繼承父類的function(可以理解為成員方法)。
  * 如果想繼承更豐富的內容,請完善clone方法。
  *
  *
  */
 /**
  * 動物(父類)
  * @type {{construct: construct, eat: eat}}
  */
 var Animal = {
     construct: function(name){
         this.name = name;
     },
     eat: function(){
         console.log("My name is "+this.name+". I can eat!");
     }
 };
 /**
  * 鳥(子類)
  * 鳥類重寫了父類eat方法,並擴充出fly方法
  * @type {子類|void}
  */
 var Bird = Animal.extend({
     eat: function(food){
         console.log("My name is "+this.name+". I can eat "+food+"!");
     },
     fly: function(){
         console.log("I can fly!");
     }
 });
 /**
  * 建立鳥類執行個體
  * @type {Jim}
  */
 var birdJim = Bird.create("Jim"),
     birdTom = Bird.create("Tom");
 birdJim.eat("worm");  //My name is Jim. I can eat worm!
 birdJim.fly();  //I can fly!
 birdTom.eat("rice");  //My name is Tom. I can eat rice!
 birdTom.fly();  //I can fly!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.