OpenLayers 項目分析(三)-OpenLayers中定製JavaScript內置類

(三)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中被刪除,可能是以前版本遺留下來的,這裏就不說它們了。

[代碼]js代碼:

01 //Test whether a string starts with another string.
02 startsWith: function(str, sub) {
03   return (str.indexOf(sub) == 0);
04   }
05  
06 //Test whether a string contains another string.
07   contains: function(str, sub) {
08       return (str.indexOf(sub) != -1);
09   }
10  
11   //Removes leading and trailing whitespace characters from a string.
12   trim: function(str) {
13       return str.replace(/^\s*(.*?)\s*$/, "$1");   
14   }
15  
16  //Camel-case a hyphenated string.
17 //Ex."chicken-head"becomes"chickenHead",
18  //and"-chicken-head"becomes"ChickenHead".
19  // “駱駝”化帶有連字符的字符串。
20  camelize: function(str) {
21       var oStringList = str.split('-');
22       var camelizedString = oStringList[0];
23       for (var i = 1; i < oStringList.length; i++) {
24           var s = oStringList[i];
25           camelizedString += s.charAt(0).toUpperCase() + s.substring(1);
26       }
27       return camelizedString;
28   }

Number:
項目僅對number類型擴展了一個方法OpenLayers. Number. limitSigDigs(還有一個方法Number. limitSigDigs,同樣在3.0中會刪除)。

[代碼]js代碼:

01 //Limit the number of significant digits on an integer.
02     limitSigDigs: function(num, sig) {
03         var fig;
04         if(sig > 0) {
05             fig = parseFloat(num.toPrecision(sig));
06         else {
07             fig = 0;
08         }
09         return fig;
10     }

Function:
擴展了兩個方法bind 和bindAsEventListener(同樣存在Function.bind和Function. bindAsEventListener兩個被“遺棄”的函數)。

[代碼]js代碼:

01 //Bind a function to an object. 
02  //Method to easily create closures with'this' altered.
03  bind: function(func, object) {
04      // create a reference to all arguments past the second one
05      var args = Array.prototype.slice.apply(arguments, [2]);
06      return function() {
07          // Push on any additional arguments from the actual function call.
08          // These will come after those sent to the bind call.
09          var newArgs = args.concat(
10              Array.prototype.slice.apply(arguments, [0])
11          );
12          return func.apply(object, newArgs);
13      };
14  }
15  
16  //Bind a function to an object, and configure it to receive the event
17  //object as first parameter when called.
18  bindAsEventListener: function(func, object) {
19      return function(event) {
20          return func.call(object, event || window.event);
21      };
22  }

裏說說這兩個方法。
首先看看bind方法,這是一個能夠被Function的實例得到的方法,如下所示:

[代碼]js代碼:

01 Function.prototype.bind = function() {
02 var _method = this, args = [], object = arguments[0];
03 for (var i = 1; i < arguments.length; i++)
04 args.push(arguments[i]);
05 return function(moreargs) {
06 for (var i = 0; i < arguments.length; i++)
07 args.push(arguments[i]);
08 return  _method.apply(object, args);
09 }
10 };

_method 代表Function實例自身,bind可接收多個參數,不過它綁定是是第一個參數,該參數是一個function或者是調用環境,後面的都是執行函數的參數。

[代碼]js代碼:

1 Function.prototype.bindAsEventListener = function(object) {
2 var  _method = this;
3 return function(event) {
4 return  _method.call(object, event || window.event);
5 }
6 };

這裏只是將object作爲_method 引用的環境,就是說現在可以在object對象中這樣使用,
object. _method (event||window.event)。
也許你注意到了Funtion擴展的兩個方法一個用到了call而另一個用的是apply,其實這兩個並沒有什麼太大的區別,只是參數傳遞的形式不同,如若沒有參數要傳遞,那麼這兩個是一樣的:
apply(obj[,argumentsArray]),call(obj[,arg1[,arg2…]])。
發佈了99 篇原創文章 · 獲贊 47 · 訪問量 49萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章