原型模式——javascript的物件導向

來源:互聯網
上載者:User

標籤:原型模式   javascript繼承   javascript類   

    javascript沒有類這個概念,但是物件導向的標誌確是擁有類概念。對於類抽象重複的解決方案在javascript中沒有辦法通過繼承來實現。但是javascript的每個函數都自動添加一個名稱為prototype屬性,這是一個對象,我們可以將所有的執行個體通過一個原型鏈引用到prototype上,從而類比類的方式。

    原型模式實現一個介面,該介面用於建立當前對象的複製,從而實現建立重複的對象。

    在javascript中的繼承關係就可以通過原型複製一份父類給子類便可。我們可以定義一個所有類型的父類Class來實現繼承的函數,所有其他類通過繼承父類。Class類的代碼如下:


function Class(){}Class.extend = function(prop){var _super = this.prototype;initializing = true;var prototype = new this();initializing = false;fnTest = /xyz/.test(function(){xyz;})?/\b_super\b/:/.*/;for(var name in prop){prototype[name] = typeof prop[name] == "function"&&typeof _super[name] == "function"&&fnTest.test(prop[name])?(function(name,fn){return function(){var tmp = this._super;this._super = _super[name];var ret = fn.apply(this,arguments);this._super = tmp;return ret;};})(name,prop[name]):prop[name];}function Class(){if(!initializing){if(!this.ctor)return;elsethis.ctor.apply(this,arguments);}}Class.prototype = prototype;Class.prototype.constructor = Class;Class.extend = arguments.callee;return Class;};

     第23行代碼重寫了Class(),定義了ctor作為建構函式,第8行到第19行根據prop的變數類型將父類的屬性和方法複製給子類,實現繼承和重載的功能,同時定義_super來實現調用父類方法的功能。

    下面是一個Class函數的應用,通過類比遊戲打怪的方式,類圖:

650) this.width=650;" src="http://s1.51cto.com/wyfs02/M02/8B/61/wKioL1hMDEbjHLfRAAApGL1RQ78824.png-wh_500x0-wm_3-wmp_4-s_3236169846.png" title="類圖.png" alt="wKioL1hMDEbjHLfRAAApGL1RQ78824.png-wh_50" />

:

650) this.width=650;" src="http://s5.51cto.com/wyfs02/M00/8B/65/wKiom1hMDLrRbbuBAABfV6CgQJ8464.png-wh_500x0-wm_3-wmp_4-s_37890131.png" title=".png" alt="wKiom1hMDLrRbbuBAABfV6CgQJ8464.png-wh_50" />

實現代碼:

<script>var mouse = null,shark = null;var player = new Player(20,500);function metMouse(){mouse = new Mouse(10,10,5,100);console.log("在("+mouse.x + "," + mouse.y +")產生老鼠");document.getElementById("mouseid").style.display = "block";setInterval(function(){if(!mouse.isDie())player.hurt(mouse.att);elseconsole.log("老鼠已經死了");},2000);}function metShark(){shark = new Shark(10,15,10,120);console.log("在("+shark.x + "," + shark.y +")產生鯊魚");document.getElementById("sharkid").style.display = "block";setInterval(function(){if(!shark.isDie())player.hurt(shark.att);elseconsole.log("鯊魚已經死了");},2000);}function attackMouse(){mouse.hurt(player);}function attackShark(){shark.hurt(player);}</script></head><body><button onclick = "metMouse()">遇見老鼠</button><button onclick = "metShark()">遇見鯊魚</button><button onclick = "attackMouse()" style = "display:none;" id = "mouseid">攻擊老鼠</button><button onclick = "attackShark()" style = "display:none;" id = "sharkid">攻擊鯊魚</button></body>

怪獸的代碼:

var Monster = Class.extend({ctor:function(_x,_y,_att,_blood){this.x = _x,this.y = _y,this.att = _att;this.blood = _blood;},attack:function(player){player.hurt(this.att);},hurt:function(player){this.blood -= player.att;console.log("怪獸受到" + player.att + "攻擊");console.log("怪獸當前血量:" + this.blood);},isDie:function(){return this.blood <= 0;}});

玩家的代碼:

var Player = Class.extend({ctor:function(_att,_blood){this.att = _att;this.blood = _blood;},hurt:function(_att){this.blood -= _att;console.log("玩家受到" + _att + "傷害");console.log("玩家當前血量為:" + this.blood);}});

Shark類

var Shark = Monster.extend({attack:function(player){_super.attack(player);player.hurt(20);console.log("鯊魚怪物給玩家造成額外的20點傷害");},hurt:function(player){console.log("鯊魚怪皮厚,減少10點傷害");this.blood = this.blood - player.att + 10;console.log("鯊魚受到" + (player.att - 10) + "點傷害");console.log("鯊魚當前血量:" + this.blood);}});


本文出自 “走一停二回頭看三” 部落格,請務必保留此出處http://janwool.blog.51cto.com/5694960/1881559

原型模式——javascript的物件導向

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.