您的位置:GIS门户网 GIS开发 正文
GIS网内容搜索
GIS网热门内容
GIS网推荐内容
GIS网最新内容
提出意见和建议

OpenLayers教程之OpenLayers中的类介绍

GIS门户网提示:本文章共1931字,分2页,当前第1页,快速翻页:
 

 

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中用来判断参数类型的。
 

收藏本页:

点这里复制本页地址发送给您QQ/MSN上的好友

相关文章

软件工程爱好者的典藏-软件工程思想
API全功略(API编程的详细介绍)
快速精通FRAME
了解Ajax
Java应用技巧
OpenLayers教程之OpenLayers的WMS调用
OpenLayers教程之解析GML文件
OpenLayers教程之空间数据的组织与实现
ArcGIS中有关MDB文件锁定的说明
迷失在专注中的Web2.0
如何打包.net framework发布[教程]
ArcObject入门教程
如何用Delphi进行ArcObjects的应用开发
ArcGIS Desktop开发基础
使用ArcGIS Engine开发应用程序[二次开发教

相关评论


GIS门户网提示:本文章所属分类:首页 GIS开发
GIS

GIS