OpenLayers.BaseType基本類型例子(API中沒有舉例的)

API中常見的用法就不說了,下面只描述API中沒有例子的,不知道怎麼使用的例子:
OpenLayers.String.format(template:String, context:Object, args:Array):String
OpenLayers.Number.limitSigDigs(num:Float, sig:int):Float
OpenLayers.Number.format(num:Float, dec:Integer, tsep:String, dsep:String):String

OpenLayers.Function.bind(func:Function, object:Object):Function
OpenLayers.Function.bindAsEventListener(func:Function, object:Object):Function
OpenLayers.Array.filter(array:Array, callback:Function, caller:Object):Array;
<html>
<head>
<title></title>
<script type="text/javascript" src="OpenLayers.js" ></script>
</head>

<body>
<!-- 以$符號的形式格式化字符串的符號,返回一個帶有符號的字符串作爲上下文的屬性。
其實就是用後面數組中的值替換帶有${}佔位符的值, OpenLayers.String.format的參數有3個,第2個爲可選 -->
<ul>
<li>
<input type="button" onclick="alert(OpenLayers.String.format('http://www.baidu.com?a=${0}&b=${1}&c=${2}', [1,2,3]))" value="OpenLayers.String.format"/>
</li>
</ul>
<ul>
<li>
<!-- 第二個參數只限制輸出的Float型的數字個數,即不含小數點的個數 -->
<input type="button" onclick="alert(OpenLayers.Number.limitSigDigs(103.1564154984, 8))" value="OpenLayers.Number.limitSigDigs"/>
</li>
</ul>

<!-- OpenLayers.Number.format -->
<!-- 返回數字格式化後的字符串表達
第一個參數是一個浮點數;
第二個參數是0或者Null,爲0,表示只保留整數部分,爲Null,則輸出含小數部分
第三個參數是千分位分隔符,看不懂的去看office
第四個參數是小數位的分隔符
-->
<ul>
<li>
<input type="button" onclick="alert(OpenLayers.Number.format(1010.032, 0, ',', '.'))" value="OpenLayers.Number.format"/>
</li>
<li>
<input type="button" onclick="alert(OpenLayers.Number.format(1010.032, null, ',', '.'))" value="OpenLayers.Number.format"/>
</li>
</ul>
</body>
</html>


這裏額外說下:OpenLayers.Function.bind(func:Function, object:Object):Function和
OpenLayers.Function.bindAsEventListener(func:Function, object:Object):Function
單獨使用跟jquery中的$(obj).bind(function(){});是有差別的哦。
要起作用還得使用事件類提供的方法(詳細查看事件類的使用),如下:
<input type="button" id="bindId" value="測試結果" />
<script type="text/javascript">
function buttonDown(){
alert('測試綁定,按下按鈕!');
}
var bid = document.getElementById("bindId");
OpenLayers.Event.observe(bid,"mousedown",OpenLayers.Function.bind(this.buttonDown,bid));
OpenLayers.Event.observe(bid,"mousedown",OpenLayers.Function.bindAsEventListener(this.buttonDown,bid));
</script>


過濾數組:

OpenLayers.Array.filter(["foo","1", "2"], function(item, index, array) {
alert(item);
});
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章