Openlayer源码阅读(一):从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()方法

转载自:https://blog.csdn.net/u013240519/article/details/81324976

You may also like...

退出移动版