JS “eval智能” 原廠模式

來源:互聯網
上載者:User

標籤:

<script>var Shop = function () {this.name = function () { document.write("商店的名字 <br/>"); },this.local = function () { document.write("商店的位置 <br/>"); }};var ToolShop = function () {ToolShop.convertASThis.constructor.call(this);this.kind = function () { document.write("賣工具的商店 <br/>"); },this.time = function () { document.write("工具商店上班時間8:00~16:00 <br/>"); }};var PetShop = function () {PetShop.convertASThis.constructor.call(this);this.kind = function () { document.write("賣貓狗的商店 <br/>"); },this.time = function () { document.write("上班時間9:00~15:00 <br/>"); }};extend(ToolShop, Shop);extend(PetShop, Shop);//=========================================================var Fish_ToolShop = function () {Fish_ToolShop.convertASThis.constructor.call(this);this.material = function () { document.write("釣魚所用的材料! <br/>"); };}var Repair_ToolShop = function () {Repair_ToolShop.convertASThis.constructor.call(this);this.material = function () { document.write("修理所用的材料! <br/>"); };}extend(Repair_ToolShop, ToolShop);extend(Fish_ToolShop, ToolShop);//=========================================================//智能工廠,動態執行個體化商店var ShopFactory = {Init: function (shopName) {var tmpObj = eval("new " + shopName + "();");return tmpObj;}};//=========================================================//不同的工具商店可能存在多種不同類型的工具,所以存在一個商店的父類但是不能被執行個體化,需要在子類中重寫var FatherToolShop = function () {this.sellTool = function (kind) {throw new Error(‘this is a abstract class‘);};};//這個對象繼承FatherToolShop後重寫sellTool來來進行執行個體var OtherToolShop = function () { };extend(OtherToolShop, FatherToolShop);//繼承之後將原型鏈中的sellTool方法重寫OtherToolShop.prototype.sellTool = function (kind) {var kind = kind;var kinds = ["Repair_ToolShop", "Fish_ToolShop"];for (var v in kinds) {if (kinds[v] == kind) {kind = ShopFactory.Init(kind);kind.kind(); //工具商店kind.time(); //上班時間break;}}return kind;};//=========================================================var test = new OtherToolShop();test.sellTool("Repair_ToolShop").material();test.sellTool("Fish_ToolShop").material();</script>

  

JS “eval智能” 原廠模式

聯繫我們

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