zoukankan      html  css  js  c++  java
  • OpenLayers 项目分析(三)OpenLayers中定制JavaScript内置类

    转自:http://www.openlayers.cn/portal.php?mod=view&aid=9

    三)BaseTypes:
    OpenLayers中定制JavaScript内置类
      OpenLayers不仅“自己”写了一些底层的类,像上回说的那些都是。同时也定制了一些JS的一些内置类,即对JS内置类的扩展。这个扩展主要包含3类:String,Number,Function,存在于BaseTypes.js文件中。
      String:
    OpenLayers对string类型定制了8个方法,分别是startsWith、contains、trim和camelize;还有另外4个方法:String.
    startsWith、String. contains、String.trim和String.
    Camelize,它们将会在3.0Version中被删除,可能是以前版本遗留下来的,这里就不说它们了。

    //Test whether a string starts with another string.
      startsWith: function(str, sub) {
        return (str.indexOf(sub) == 0);
        }

      //Test whether a string contains another string.
        contains: function(str, sub) {
            return (str.indexOf(sub) != -1);
        }

        //Removes leading and trailing whitespace characters from a string.
        trim: function(str) {
            return str.replace(/^\s*(.*?)\s*$/, "$1");   
        }

       //Camel-case a hyphenated string.
      //Ex."chicken-head"becomes"chickenHead",
       //and"-chicken-head"becomes"ChickenHead".
       // “骆驼”化带有连字符的字符串。
       camelize: function(str) {
            var oStringList = str.split('-');
            var camelizedString = oStringList[0];
            for (var i = 1; i < oStringList.length; i++) {
                var s = oStringList[i];
                camelizedString += s.charAt(0).toUpperCase() + s.substring(1);
            }
            return camelizedString;
        }

    Number:
    项目仅对number类型扩展了一个方法OpenLayers. Number. limitSigDigs(还有一个方法Number.
    limitSigDigs,同样在3.0中会删除)。

    //Limit the number of significant digits on an integer.
        limitSigDigs: function(num, sig) {
            var fig;
            if(sig > 0) {
                fig = parseFloat(num.toPrecision(sig));
            } else {
                fig = 0;
            }
            return fig;
        }
    Function:
    扩展了两个方法bind 和bindAsEventListener(同样存在Function.bind和Function.
    bindAsEventListener两个被“遗弃”的函数)。
    //Bind a function to an object.  
     //Method to easily create closures with'this' altered.
     bind: function(func, object) {
         // create a reference to all arguments past the second one
         var args = Array.prototype.slice.apply(arguments, [2]);
         return function() {
             // Push on any additional arguments from the actual function call.
             // These will come after those sent to the bind call.
             var newArgs = args.concat(
                 Array.prototype.slice.apply(arguments, [0])
             );
             return func.apply(object, newArgs);
         };
     }
    
     //Bind a function to an object, and configure it to receive the event
     //object as first parameter when called. 
     bindAsEventListener: function(func, object) {
         return function(event) {
             return func.call(object, event || window.event);
         };
     }
    这里说两个方法。
    首先看看bind方法,这是一个能够被Function的实例得到的方法,如下所示:
    Function.prototype.bind = function() {
    var _method = this, args = [], object = arguments[0];
    for (var i = 1; i < arguments.length; i++)
    args.push(arguments[i]);
    return function(moreargs) {
    for (var i = 0; i < arguments.length; i++)
    args.push(arguments[i]);
    
    return _method.apply(object, args); } };
    _method
    代表Function实例自身,bind可接收多个参数,不过它绑定是是第一个参数,该参数是一个function或者是调用环境,后面的都是执行函数的参数。
    Function.prototype.bindAsEventListener = function(object) {
    var  _method = this;
    return function(event) {
    return  _method.call(object, event || window.event);
    }
    };
    这里只是将object作为_method 引用的环境,就是说现在可以在object对象中这样使用,
    object. _method
    (event||window.event)。
    也许你注意到了Funtion扩展的两个方法一个用到了call而另一个用的是apply,其实这两个并没有什么太大的区别,只是参数传递的形式不同,如若没有参数要传递,那么这两个是一样的:
    apply(obj[,argumentsArray]),call(obj[,arg1[,arg2…]])。
  • 相关阅读:
    分布式数据库管理系统
    Java并发(一)Java并发/多线程教程
    nginx重启后,反向代理失败之问题排查记录
    从spring源码汲取营养:模仿spring事件发布机制,解耦业务代码
    Mybatis中多表关联时,怎么利用association优雅写resultMap来映射vo
    曹工杂谈:为什么很少需要改Spring源码,因为扩展点太多了,说说Spring的后置处理器
    fastjson自由:controller上指定active profile,让你想序列化什么字段就序列化什么字段
    就因为加了Lombok的@Accessors(chain = true),bean拷贝工具类不干活了
    宽带爱折腾-将家里光猫转成桥接模式
    修改springfox-swagger源码,使example中时间格式默认为“yyyy-MM-dd HH:mm:ss”
  • 原文地址:https://www.cnblogs.com/LCGIS/p/3048321.html
Copyright © 2011-2022 走看看