OpenLayers教程之OpenLayers中的类介绍

OpenLayers教程之OpenLayers中的类介绍:

OpenLayers是典型的面向对象脚本。对于没有研究过开源脚本的人来说,OpenLayers是不怎么好阅读的,而且目前没有什么说明其思想和架构方面的官方文档。 字串2

我们来说说其书写格式中最大的特点,那就是对{}的应用,其实就是hash表,我们摘取其中的一段代码来说说,比如:
OpenLayers.Geometry.prototype = {

字串6

/** @type String */
id: null, 字串2

/** This is set when a Geometry is added as Component of another Geometry
*
* @type OpenLayers.Geometry */
parent: null, 字串6

/** @type OpenLayers.Bounds */
bounds: null, 字串9

initialize: function() {
this.id = OpenLayers.Util.createUniqueID(this.CLASS_NAME+ “_”);
}, 字串6

CLASS_NAME: “OpenLayers.Geometry”
};
实际上,我们把这段代码翻译过来就是:
OpenLayers.Geometry.prototype.id=null;
OpenLayers.Geometry.prototype.parent =null;
OpenLayers.Geometry.prototype.bounds =null;
OpenLayers.Geometry.prototype.initialize=function() {this.id = OpenLayers.Util.createUniqueID(this.CLASS_NAME+ “_”);};
OpenLayers.Geometry.prototype.CLASS_NAME=”OpenLayers.Geometry”;

字串4

只不过OpenLayers本身的写法精简了代码,需要注意的一点就是,这里得到的全部都是动态方法,是可以被OpenLayers.Geometry类的实例访问到,可以被其子类继承的。

字串7

另外一个就是[]的应用,是数组数组或者用来描述属性:

字串2

例如:
OpenLayers.Util.extend = function(destination, source) {
for (property in source) {
destination[property] = source[property];
}
return destination;
};
这个静态方法就是把source中的每个属性和方法绑定到destination中,假如destination中已经有该属性和方法则仅仅只会改变其值,否则会创建一个该属性和方法。
在OpenLayers.js中用OpenLayers = new Object();得到了一个Object的对象,而实际上这个东西是当作一个命名空间来使用的,诸如OpenLayers._scriptName,OpenLayers._getScriptLocation()等都属于该空间中的静态属性方法,是可以直接调用的。

字串5

对类的实现比较重要的一个类是OpenLayers.Class ,其中实现了 create和inherit方法,我们首先来分析一下其中的代码。 字串5

OpenLayers.Class = {
isPrototype: function () {}, // magic anonymous value 字串6

create: function() {
return function() {
if (arguments && arguments[0] != OpenLayers.Class.isPrototype)
this.initialize.apply(this, arguments);
}
}, 字串3

inherit: function () {
var superClass = arguments[0];
var proto = new superClass(OpenLayers.Class.isPrototype);
for (var i = 1; i < arguments.length; i++) {
if (typeof arguments[i] == “function”) {
var mixin = arguments[i];
arguments[i] = new mixin(OpenLayers.Class.isPrototype);
}
OpenLayers.Util.extend(proto, arguments[i]); 字串7

// This is a hack for IE see
// http://trac.openlayers.org/attachment/ticket/552
//
// The problem is that ie doesnt recognize toString as a property
// so the util.extend() doesnt copy it over. we do it manually.
//
// to be revisited in 3.0
//
if((arguments[i].hasOwnProperty && arguments[i].hasOwnProperty(’toString’)) ||
(!arguments[i].hasOwnProperty && arguments[i].toString)) {
proto.toString = arguments[i].toString;
}
}
return proto;
}
}; 字串7

isPrototype属性仅仅是用一个函数的壳子,在create中用来判断参数类型的。 字串4

Create方法直接返回了一个函数,的执行结果,它用apply方法调用了对象自身的initialize方法,那么在实际的应用中,如:OpenLayers.Renderer.Elements = OpenLayers.Class.create();这句就执行了OpenLayers.Renderer.Elements某个实例中的initialize方法,而这个方法的实际上就是一个类的构造函数。一句话就是,Create实现了构造函数。

字串5

而大多数和DOM节点扯上关系的类都实现了destroy方法,该方法用于解除对象和节点之间的绑定关系并回收内存的,这个相当于类的析构函数。一句话,destroy实现了析构函数。
而inherit则实现了类的继承。首先我们通过for (var i = 1; i < arguments.length; i++)可以看到inherit是可以接收多个参数的,其中第一个参数被实例化了,因为在for循环中,我们需要用OpenLayers.Util.extend(proto, arguments[i]);方法来实现具体的继承,而这个方法的应用如果不通过类的实例,我们是访问不到类中的动态方法和属性的。 字串3

转载自:https://blog.csdn.net/king2232/article/details/3323769

You may also like...