معلومات عن المنتج:
دالة Hashtable () // قم بتعيين hashtable
{
this._hash = new Object();
this.add = function(key, value) {
إذا (نوع (مفتاح) ! = "غير محدد") {
إذا (هذا. يحتوي على (مفتاح) == خطأ) {
this._hash[key] = typeof (value) == "غير محدد"؟ فارغة: القيمة؛
عودة صحيحة؛
} آخر {
عودة كاذبة.
}
} آخر {
عودة كاذبة.
}
}
this.remove = function(key) { احذف this._hash[key]; }
this.count = function() { var i = 0; for (var k in this._hash) { i++; } إرجاع أنا؛ }
this.items = function(key) { return this._hash[key]; }
this.contains = function(key) { return typeof (this._hash[key]) != "undef"; }
this.clear = function() { for (var k in this._hash) { احذف this._hash[k]; } }
}
معلومات عن المنتج:
// js哈希表
الدالة HashTable() {
this.ObjArr = {};
this.Count = 0;
// 添加
this.Add = function(key, value) {
إذا (this.ObjArr.hasOwnProperty(key)) {
عودة كاذبة. //الحصول على أفضل النتائج
}
آخر {
this.ObjArr[key] = value;
this.Count++;
عودة صحيحة؛
}
}
//是否包含某项
يحتوي على = وظيفة(مفتاح) {
return this.ObjArr.hasOwnProperty(key);
}
// قم بالبحث عن هذا المفتاح this.ObjArr[key]
this.GetValue = وظيفة(مفتاح) {
إذا (هذا. يحتوي على (مفتاح)) {
إرجاع this.ObjArr[key];
}
آخر {
throw Error("Hashtable لا يحتوي على المفتاح:" + String(key)); //المصدر
//يعود؛
}
}
// 移除
هذا.إزالة = وظيفة (مفتاح) {
إذا (هذا. يحتوي على (مفتاح)) {
احذف this.ObjArr[key];
this.Count--;
}
}
// 清空
هذا.مسح = وظيفة () {
this.ObjArr = {}; this.Count = 0;
}
}
الوصف:
//员工
وظيفة الموظف (المعرف، اسم المستخدم) {
this.id = id;
this.userName = userName;
}
اختبار الوظيفة () {
var ht = new HashTable();
var tmpEmployee = null;
لـ (var i = 1; i < 6; i++) {
tmpEmployee = موظف جديد(i, "Employee_" + i);
ht.Add(i, tmpEmployee);
}
لـ (var i = 1; i <= ht.Count; i++) {
تنبيه (ht.GetValue(i).userName); //其实等价于ht.ObjArr[i].userName
//alert(ht.ObjArr[i].userName);
}
ht.Remove(1);
تنبيه(ht.Contains(1)); //خطأ شنيع
تنبيه(ht.Contains(2)); //حقيقي
// تنبيه (ht.GetValue (1))؛ //异常
نتيجة فار = ht.GetValue(2);
إذا (النتيجة ! = فارغة) {
تنبيه ("معرف الموظف:" + result.id + "؛ اسم المستخدم:" + result.userName)؛
}
ht.Add(2, "المفتاح هو مفتاح 已经存在!"); //أضف المحتوى
//ht.Clear(); // 清空
تنبيه (ht.Count)؛
}