|
@@ -482,3 +482,148 @@
|
|
|
|
|
|
personForm(document.body, b)
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// getSetForm
|
|
|
+// Сделайте функцию, которая решает предыдущую задачу универсально, то есть для любого объекта, в котором есть набор функций get... и set.... Кода станет меньше, гибкости больше, ведь в предыдущем задании много копипасты(x5)
|
|
|
+{
|
|
|
+ function getSetForm(parent, getSet) {
|
|
|
+ const inputs = {} //реестр
|
|
|
+
|
|
|
+ const updateInputs = () => {
|
|
|
+ for (const fieldName in inputs) {
|
|
|
+ const getKey = `get` + fieldName
|
|
|
+ if (getKey in getSet) {
|
|
|
+ inputs[fieldName].value = getSet[getKey]() || ''
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for (const getSetName in getSet) {
|
|
|
+ const getOrSet = getSetName.slice(0, 3)
|
|
|
+ const fieldName = getSetName.slice(3)
|
|
|
+ const setKey = `set` + fieldName
|
|
|
+ const getKey = `get` + fieldName
|
|
|
+
|
|
|
+ if (!(fieldName in inputs)) {
|
|
|
+ inputs[fieldName] = document.createElement('input')
|
|
|
+ parent.append(inputs[fieldName])
|
|
|
+ if (!(setKey in getSet)) {
|
|
|
+ inputs[fieldName].disabled = true
|
|
|
+ }
|
|
|
+
|
|
|
+ inputs[fieldName].type = typeof getSet[getKey]()
|
|
|
+ inputs[fieldName].placeholder = fieldName
|
|
|
+ inputs[fieldName].oninput = () => {
|
|
|
+ inputs[fieldName].value = getSet[setKey](inputs[fieldName].value)
|
|
|
+ updateInputs()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ updateInputs()
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // проверка
|
|
|
+
|
|
|
+
|
|
|
+ function createPersonClosure(name, surName) {
|
|
|
+ let fatherName, age
|
|
|
+
|
|
|
+ return person = {
|
|
|
+ getName() {
|
|
|
+ return name
|
|
|
+ },
|
|
|
+ getSurname() {
|
|
|
+ return surName
|
|
|
+ },
|
|
|
+ getFatherName() {
|
|
|
+ return fatherName
|
|
|
+ },
|
|
|
+ getAge() {
|
|
|
+ return age
|
|
|
+ },
|
|
|
+ getFullName() {
|
|
|
+ return `${name} ${fatherName} ${surName}`
|
|
|
+ },
|
|
|
+
|
|
|
+ setName(newName) {
|
|
|
+ if (newName[0] === newName[0].toUpperCase()) {
|
|
|
+ name = newName
|
|
|
+ }
|
|
|
+ return name
|
|
|
+ },
|
|
|
+ setSurname(newSurname) {
|
|
|
+ if (newSurname[0] === newSurname[0].toUpperCase()) {
|
|
|
+ surName = newSurname
|
|
|
+ }
|
|
|
+ return surName
|
|
|
+ },
|
|
|
+ setFatherName(newFatherName) {
|
|
|
+ if (newFatherName[0] === newFatherName[0].toUpperCase()) {
|
|
|
+ fatherName = newFatherName
|
|
|
+ }
|
|
|
+ return fatherName
|
|
|
+ },
|
|
|
+ setAge(newAge) {
|
|
|
+ if (newAge >= 0 && newAge <= 100) {
|
|
|
+ age = newAge
|
|
|
+ }
|
|
|
+ return age
|
|
|
+ },
|
|
|
+ setFullName(newFullName) {
|
|
|
+ name = newFullName.split(' ')[1] || ''
|
|
|
+ surName = newFullName.split(' ')[0] || ''
|
|
|
+ fatherName = newFullName.split(' ')[2] || ''
|
|
|
+ return `${name} ${fatherName} ${surName}`
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ let car;
|
|
|
+ {
|
|
|
+ let brand = 'BMW', model = 'X5', volume = 2.4
|
|
|
+ car = {
|
|
|
+ getBrand() {
|
|
|
+ return brand
|
|
|
+ },
|
|
|
+ setBrand(newBrand) {
|
|
|
+ if (newBrand && typeof newBrand === 'string') {
|
|
|
+ brand = newBrand
|
|
|
+ }
|
|
|
+ return brand
|
|
|
+ },
|
|
|
+
|
|
|
+ getModel() {
|
|
|
+ return model
|
|
|
+ },
|
|
|
+ setModel(newModel) {
|
|
|
+ if (newModel && typeof newModel === 'string') {
|
|
|
+ model = newModel
|
|
|
+ }
|
|
|
+ return model
|
|
|
+ },
|
|
|
+
|
|
|
+ getVolume() {
|
|
|
+ return volume
|
|
|
+ },
|
|
|
+ setVolume(newVolume) {
|
|
|
+ newVolume = +newVolume
|
|
|
+ if (newVolume && newVolume > 0 && newVolume < 20) {
|
|
|
+ volume = newVolume
|
|
|
+ }
|
|
|
+ return volume
|
|
|
+ },
|
|
|
+
|
|
|
+ getTax() {
|
|
|
+ return volume * 100
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ getSetForm(document.body, car)
|
|
|
+ getSetForm(document.body, createPersonClosure('Анон', "Анонов"))
|
|
|
+}
|