Skip to content

Instantly share code, notes, and snippets.

@sashasushko
Last active August 3, 2020 17:02
Show Gist options
  • Save sashasushko/9f8409ba509136ad9565f2358af2f255 to your computer and use it in GitHub Desktop.
Save sashasushko/9f8409ba509136ad9565f2358af2f255 to your computer and use it in GitHub Desktop.
Сравнение самописных get/set против нативных
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