OpenLayers源碼閱讀(一):從ol開始

版本選擇 v4.6.5
說明: v3.0.0~v4.6.5採用Closure,而從v5.0.0 之後採用ES Modules。

源碼地址:https://github.com/openlayers/openlayers/tree/v4.6.5
openlayers從ol開始,對應文件https://github.com/openlayers/openlayers/blob/v4.6.5/src/ol/index.js
在index.js文件中,主要做了兩件事:
第一件事是:定義了25個屬性名並賦初始值,主要有:

  • ol.ASSUME_TOUCH = false;
  • ol.DEFAULT_MAX_ZOOM = 42; //最大級別
  • ol.DEFAULT_MIN_ZOOM = 0; //最小級別
  • ol.DEFAULT_RASTER_REPROJECTION_ERROR_THRESHOLD = 0.5;
  • ol.DEFAULT_TILE_SIZE = 256;//瓦片像素大小
  • ol.DEFAULT_WMS_VERSION = ‘1.3.0’;//WMS服務版本
  • ol.ENABLE_CANVAS = true;//支持canvas
  • ol.ENABLE_PROJ4JS = true;//支持proj4
  • ol.ENABLE_WEBGL = true;//支持webGL
    ……

第二件事是:定義了2個方法

  • ol.inherits //對象繼承
  • ol.getUid //獲取對象id
    其中,ol.inherits貫穿於openlayers的所有對象,該方法內容如下:
/**
 * Inherit the prototype methods from one constructor into another.
 *
 * Usage:
 *
 *     function ParentClass(a, b) { }
 *     ParentClass.prototype.foo = function(a) { }
 *
 *     function ChildClass(a, b, c) {
 *       // Call parent constructor
 *       ParentClass.call(this, a, b);
 *     }
 *     ol.inherits(ChildClass, ParentClass);
 *
 *     var child = new ChildClass('a', 'b', 'see');
 *     child.foo(); // This works.
 *
 * @param {!Function} childCtor Child constructor.
 * @param {!Function} parentCtor Parent constructor.
 */
ol.inherits = function(childCtor, parentCtor) {
  childCtor.prototype = Object.create(parentCtor.prototype);
  childCtor.prototype.constructor = childCtor;
};

openlayers是採用javascript編寫的,而javascript是基於原型的。
這裏寫圖片描述
所以,子對象的原型繼承於父對象的原型,子對象的構成函數是其本身。

擴展閱讀:https://msdn.microsoft.com/zh-cn/library/hh924508(v=vs.94).aspx
擴展閱讀:Js的apply()和call()的異同點,object.create()方法

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章