Last active
July 14, 2022 19:07
-
-
Save evgeniyworkbel/f39c01d83785b4058ea2b0dcb1ceae49 to your computer and use it in GitHub Desktop.
Курс "JS: Объектно-ориентированный дизайн" (Хекслет)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Объекты JavaScript позволяют обращаться к собственным свойствам. При обращении к свойству, которое не было установлено, возвращается undefined: | |
const obj = { | |
key: 'value', | |
key2: { | |
key3: 'value3', | |
}, | |
}; | |
obj.key2 // { key3: 'value3' } | |
obj.key2.key1 // undefined | |
obj.key2.key1.key0 // Uncaught TypeError: Cannot read property 'key0' of undefined | |
В этом испытании мы реализуем особый объект, позволяющий обращаться к несуществующим свойствам, не получая ошибки. | |
object.js | |
Реализуйте и экспортируйте по умолчанию функцию, которая принимает объект и позволяет получать из него свойства по любому имени. При обращении к несуществующему свойству не должно выбрасываться исключений или возвращаться undefined. Функция должна возвращать объект Proxy. | |
import createObject from './object.js'; | |
const obj = createObject({ | |
key: 'value', | |
key2: { | |
key3: 'value3', | |
}, | |
}); | |
obj.key2 // { key3: 'value3' } | |
// код корректно продолжает работу: | |
obj.key2.key1 | |
obj.key2.key1.key0 | |
obj.obj.obj | |
Подсказки | |
Урок "Прокси" https://ru.hexlet.io/courses/js-object-oriented-design/lessons/proxy/theory_unit | |
Документация по Proxy на MDN https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Proxy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment