cc.Class({ name: 'ConfigBase', properties: { mainKey: "", indexsCache:null, multiple:[], multipleCache:null, table:null, }, ctor(table, mainKey, multipleKey) { this.table = table; this.mainKey = mainKey; this.multiple = multipleKey; this.indexsCache = {}; this.multipleCache = {}; }, // getObjectValue(object, key) { // if (object.hasOwnProperty(key)) // { // return object[key] // } // return undefined; // }, getByMainKey(idx) { let data = this.indexsCache[idx] if (data != undefined) { return data; } for (let d of this.table) { if (d[this.mainKey] == idx) { data = d; break; } } this.indexsCache[data[this.mainKey]] = data; return data; }, getByMultipleKey() { if (arguments.length != this.multiple.length) { if (arguments.length <= 0) { cc.log("multiple key is null"); } else { cc.log("multiple key num not match curNum :" + arguments.length + " need num " + this.multiple.length); } return null; } let curCache = this.findMultipleCache(arguments); if (curCache != null) { return curCache; } return this.getAndAddMultiple(arguments); }, getAndAddMultiple(args) { var result = []; // 结果拿出来 for (let d of this.table) { let isMatch = true; for (let i = 0; i < this.multiple.length; i++) { const k = this.multiple[i]; if (d[k] != args[i]) { isMatch = false; break } } if (isMatch) { result.push(d); } } // 添加到缓存区 let curCache = this.multipleCache; for (let i = 0; i < args.length - 1; i++) { let k = args[i]; let tmpCache = curCache[k]; if (tmpCache == undefined) { curCache[k] = tmpCache = {} } curCache = tmpCache; } curCache[args[args.length - 1]] = result; return result; }, findMultipleCache(args) { // 索引缓存 let curCache = this.multipleCache; let isFindCache = true; for(let p of args) { if (curCache[p] == undefined) { isFindCache = false break } curCache = curCache[p] } if (isFindCache) { return curCache; } return null; }, })