<script type="text/javascript"> function getKeyVal() { var _KeyVal = new Object(); _KeyVal.set = function (key, value) { if (arguments.length >= 2) { this[arguments[0]] = arguments[1]; } }; _KeyVal.get = function (key) { if (this.hasOwnProperty(arguments[0])) { return this[arguments[0]]; } }; _KeyVal.remove = function (key) { if (this.hasOwnProperty(arguments[0])) { delete this[arguments[0]]; } }; _KeyVal.clear = function () { for (var e in this) { delete this[e]; } }; _KeyVal.ishav = function (key) { return this.hasOwnProperty(arguments[0]); }; _KeyVal.Vals = function () { var valarr = new Array(); for (var v in this) { if (typeof (this[v]) != "function") { valarr.push(v); } } return valarr; }; _KeyVal.Keys = function () { var keyarr = new Array(); for (var k in this) { if (typeof (this[k]) != "function") { keyarr.push(this[k]); } } return keyarr; }; return _KeyVal; } var KeyVal = getKeyVal(); //获取键值对对象 KeyVal.set("l1", "11"); //添加键值对 参数1:键名,参数2:键值 KeyVal.set("l2", "22"); //添加键值对 参数1:键名,参数2:键值 KeyVal.set("l1", "33"); //添加键值对 参数1:键名,参数2:键值 var a1 = KeyVal.ishav("l1"); //查看键名是否存在 有返回true var a2 = KeyVal.get("l1"); //获取给定键的值 KeyVal.remove("l1"); //删除给定键名的键 var a3 = KeyVal.ishav("l1"); //查看键名是否存在 没有返回false var a4 = KeyVal.get("l1"); //获取给定键的值 键不存在返undefined //KeyVal.clear(); //清除所有属性 方法也会清除不好用 KeyVal.set("l3", "66"); //添加键值对 参数1:键名,参数2:键值 var arr1 = KeyVal.Vals();//返回值得数组 var arr2 = KeyVal.Keys();//返回键名的数组 var abc = "abc"; </script>