Last active
August 3, 2020 17:02
-
-
Save sashasushko/9f8409ba509136ad9565f2358af2f255 to your computer and use it in GitHub Desktop.
Сравнение самописных get/set против нативных
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
const oldGetSet = { | |
_value: "Old", | |
getValue: function () { | |
return this._value; | |
}, | |
setValue: function (newValue) { | |
this._value = newValue; | |
} | |
}; | |
oldGetSet.getValue(); // "Old" | |
oldGetSet.setValue("Very old!"); | |
const newGetSet = { | |
_value: "New", | |
get value () { | |
return this._value; | |
}, | |
set value (newValue) { | |
this._value = newValue; | |
} | |
}; | |
newGetSet.value; // "New" | |
newGetSet.value = "Brand new!"; | |
/* | |
Конечно, иногда достаточно обычных свойств объекта | |
без каких-либо геттеров-сеттеров | |
const obj = { | |
value: "" | |
}; | |
obj.value; // "" | |
obj.value = "Regular"; | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment