JavaScript中用數組實現索引值對__Java

來源:互聯網
上載者:User
轉自: http://jsj.52zxw.net/article_view.php/99.html
vbs中有dictionary對象,js中也有相應的對象,可js的dictionary對象不是跨瀏覽器的,所以用數組實現了索引值對的功能。

  今天寫瀏覽器端js程式,需要用到索引值對的功能,vbs中有dictionary對象,js中也應該有對應的dictionary對象,查了一下js手冊,js中果然有dictionary對象,程式寫好了,跨瀏覽器一測試,發現只有IE支援new ActiveXObject("Scripting.Dictionary"),其他瀏覽器或許有相容寫法,支援dictionary對象,網上搜尋了一下,沒有搜到,突然想到js的數組,查了一些資料,實現了我想要的功能,跨瀏覽器測試一下,Google、Firefox、opera都相容,以下是程式碼,參考了網上的一個例子,稍作了下修改:

<script type="text/javascript" language="javascript">
//自訂字典對象
function Dictionary(){
 this.data = new Array();
 
 this.put = function(key,value){
  this.data[key] = value;
 };

 this.get = function(key){
  return this.data[key];
 };

 this.remove = function(key){
  this.data[key] = null;
 };
 
 this.isEmpty = function(){
  return this.data.length == 0;
 };

 this.size = function(){
  return this.data.length;
 };
}

//使用 例子
var d = new Dictionary();
d.put("CN", "China");
d.put("US", "America");
document.write(d.get("CN"));
</script>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.