This article mainly introduces the relevant information about javascript custom objects to implement the Map object function in Java. This function is implemented here to help everyone understand this part of the content. Friends in need can refer to it
javascript Custom objects implement the Map object function in Java
There are collection, Map and other object storage tool classes in Java. These objects are easy to use, but in JavaScript, you can only use Array objects .
Here I create a custom object. This object contains an array to store data. The data object is a Key, which can actually store the content!
Here Key, you have to use the String type. Just like Java, you can perform some operations such as adding, deleting, modifying, and obtaining.
It’s very simple to use. Let me show you the tool class first:
/** * @version 1.0 * @author cuisuqiang@163.com * 用于实现页面 Map 对象,Key只能是String,对象随意 */ var Map = function(){ this._entrys = new Array(); this.put = function(key, value){ if (key == null || key == undefined) { return; } var index = this._getIndex(key); if (index == -1) { var entry = new Object(); entry.key = key; entry.value = value; this._entrys[this._entrys.length] = entry; }else{ this._entrys[index].value = value; } }; this.get = function(key){ var index = this._getIndex(key); return (index != -1) ? this._entrys[index].value : null; }; this.remove = function(key){ var index = this._getIndex(key); if (index != -1) { this._entrys.splice(index, 1); } }; this.clear = function(){ this._entrys.length = 0;; }; this.contains = function(key){ var index = this._getIndex(key); return (index != -1) ? true : false; }; this.getCount = function(){ return this._entrys.length; }; this.getEntrys = function(){ return this._entrys; }; this._getIndex = function(key){ if (key == null || key == undefined) { return -1; } var _length = this._entrys.length; for (var i = 0; i < _length; i++) { var entry = this._entrys[i]; if (entry == null || entry == undefined) { continue; } if (entry.key === key) {//equal return i; } } return -1; }; }
If you don’t understand objects in Js For some basic knowledge such as creation, you can check it online.
// 自定义Map对象 var map = new Map(); map.put("a","a"); alert(map.get("a")); map.put("a","b"); alert(map.get("a"));
Pop a first and then b , because the latter one will overwrite the previous one!
The above is the detailed content of Example analysis of how JavaScript implements the Map object function in Java. For more information, please follow other related articles on the PHP Chinese website!