字典是什么 ?

字典也就是一种类似于数据的数据结构,他的出现就是为了,前端做输入框的时候限制用户输入的值,只不过字典这个数组中的元素是以 key:value的形式存在的;

  1. // 字典
  2. class Dictionary {
  3. constructor(){
  4. this.store = [];
  5. }
  6. // 查找
  7. get(key){
  8. return this.store[key];
  9. }
  10. // 添加
  11. set(key,val){
  12. this.store[key] = val;
  13. }
  14. // 包含
  15. has(key){
  16. return this.store.includes(key);
  17. }
  18. // 删除
  19. delete(key){
  20. // 如果不包含返回false
  21. if(!this.has(key)) return false;
  22. delete this.store[key];
  23. }
  24. show(){
  25. console.log(this.store);
  26. }
  27. }
  28. let a = new Dictionary();
  29. a.set('name','xtt');
  30. a.set('age','18');
  31. a.show();
  32. // 古有诸葛亮fuz