欢迎您来到GIS动力

加入收藏 免费注册 用户登陆 帮助中心
首页 新闻动态 技术专栏 银杏树下 学习考研 软件下载 求职招聘 许愿瓶 节日祝福 用户中心 精彩推荐 资源搜索 地图
专栏导航: AO开发 | SO开发 | ArcGIS桌面 | 超图桌面 | 开发语言 | 数据库 | WebGIS | 银杏文学 | 研究生考题 | FreeMap 谈天说地
   您现在位于: 首页技术专栏开源GIS → 正文
OpenLayers教程之OpenLayers中的类介绍
08-01-02 00:00:00 作者:FRONT-END STUDIO 出处:FRONT-END STUDIO

OpenLayers教程之OpenLayers中的类介绍:

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

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

/** @type String */
id: null,

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

/** @type OpenLayers.Bounds */
bounds: null,

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

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”;

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

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

例如:
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()等都属于该空间中的静态属性方法,是可以直接调用的。

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

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

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

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]);

// 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;
}
};

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

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

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


(本文已被浏览 次)
发布人:admin
推荐给好友:发送给好友
上篇新闻:
下篇新闻:
相关评论
发表我的评论
  • 尊重网上道德,遵守《全国人大常委会关于维护互联网安全的决定》及中华人民共和国其他各项有关法律法;
  • 本站有权保留或删除您发表的任何评论内容;
  •   相关文章  
    OpenLayers教程之空间数据的组织与实现
    OpenLayers教程之解析GML文件
    OpenLayers教程之OpenLayers的WMS调用

    关于我们 友情链接 ┋ 与我在线 ┋ 管理 ┋ TOP
    网站当前版本:GisPower CMS V3.0
    『GIS 动力』- http://www.gispower.org/
    联系我们:webmaster#gispower.org
    Copyright (c) 2003-2007 GisPOwer.Org. All Rights Reserved.

                   滇ICP备05006901号