oop-closures.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //Password
  2. function Password(parent = document.body, open = false){
  3. const input = document.createElement('input');
  4. const btn = document.createElement('button');
  5. parent.append(input, btn);
  6. let value = '';
  7. let readProp = open;
  8. btn.innerText = readProp ? 'show' : 'hide' ;
  9. this.setValue = newValue => {
  10. if (newValue != ' ' && newValue && newValue !== value)
  11. value = newValue
  12. }
  13. this.setOpen = newOpen => {
  14. if (newOpen != readProp && newOpen != undefined)
  15. readProp = newOpen
  16. }
  17. input.oninput = this.onChange = (input) => {
  18. if (typeof this.onChange === 'function' && input.data !== value)
  19. this.onChange(input.data)
  20. value = input.data;
  21. }
  22. btn.onclick = this.onOpenChange = (newOpen) => {
  23. if (typeof this.onChange === 'function')
  24. this.onOpenChange(readProp);
  25. readProp = !readProp;
  26. btn.innerText = readProp ? 'show' : 'hide' ;
  27. }
  28. this.getValue = () => value;
  29. this.getOpen = () => readProp;
  30. }
  31. let p = new Password(root, true)
  32. p.onChange = data => console.log(data)
  33. p.onOpenChange = open => console.log(open)
  34. //p.setValue('qwerty')
  35. console.log(p.getValue())
  36. p.setOpen(true)
  37. //console.log(p.getOpen())