js 設計模式學習(1)

來源:互聯網
上載者:User

標籤:not   方式   satellite   and   type   做了   init   style   normal   

最近一個人外出旅行 順便在那座城市買了一本關於JS設計模式的書 供無聊的時候閱讀

卻發現小有收穫 旅遊回來以後立馬寫下心得 做了一個地圖 並以此總結此次旅行的經過的地方

嘗試1:

嘗試寫JS類

 1 var TianDiTu = function (lat, lng, zoom) { 2             if (this instanceof TianDiTu) { 3                 var zoomSize = { maxZoom: 18, minZoom: 5 }, 4                 normalm = L.tileLayer.chinaProvider(‘TianDiTu.Normal.Map‘, zoomSize), 5                 normala = L.tileLayer.chinaProvider(‘TianDiTu.Normal.Annotion‘, zoomSize), 6                 imgm = L.tileLayer.chinaProvider(‘TianDiTu.Satellite.Map‘, zoomSize), 7                 imga = L.tileLayer.chinaProvider(‘TianDiTu.Satellite.Annotion‘, zoomSize); 8  9                 this.normal = L.layerGroup([normalm, normala]);10                 this.image = L.layerGroup([imgm, imga]);11                 this.center = [lat, lng];12                 this.zoom = zoom;13             } else {14                 return new TianDiTu(lat, lng, zoom);15             }16         };17 18         var tdt = TianDiTu(34.618129, 112.454059, 12);

 

嘗試2:

嘗試將調用地圖的方式放入原廠模式:

 1 var CreateMap = function () { 2             if (!(this instanceof CreateMap)) 3                 return new CreateMap(); 4         } 5         CreateMap.prototype = { 6             init: function () { 7                     map = L.map("map", { 8                     center: tdt.center, 9                     zoom: tdt.zoom,10                     layers: [tdt.normal]11                 });12 13                 return this;14             },15             createControl: function () {16                 var baseLayers = {17                     "地圖": tdt.normal,18                     "影像": tdt.image,19                 }20 21                 var overlayLayers = {22                 }23 24                 L.control.layers(baseLayers, overlayLayers).addTo(map);25             }26         }27         28         var createMap = new CreateMap();29         createMap.init().createControl();

 

js 設計模式學習(1)

聯繫我們

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