function Password(parent, open) { const input = document.createElement('input'); input.type = 'password'; parent.appendChild(input); const button = document.createElement('button'); button.type = 'button'; button.textContent = 'показать'; parent.appendChild(button); button.addEventListener('click', () => { this.setOpen(open !== true); }); this.setValue = newValue => input.value = newValue; this.getValue = () => input.value; this.setOpen = openUpdate => { open = openUpdate; this.onOpenChange(openUpdate); button.textContent = (openUpdate) ? 'показать' : 'скрыть'; input.type = (openUpdate) ? 'password' : 'text'; } this.getOpen = () => open; input.addEventListener('input', event => { if (typeof this.onChange === 'function'){ this.onChange(event.target.value); } }); this.onOpenChange = isOpen => isOpen; } let p = new Password(document.body, true); p.onChange = data => console.log(data); p.onOpenChange = open => console.log(open); p.setValue('qwerty'); console.log(p.getValue()); p.setOpen(false); console.log(p.getOpen());