JSFiddle - React, Tailwind, and code Playground
JavaScript
function addProperty(object, name, initialValue) {
//Просто попытка
var initvalue = initialValue;
object['set' + addProperty.upperCaseFirstLetter(name)] = function(value) {
initvalue = value;
};
object['get' + addProperty.upperCaseFirstLetter(name)] = function() {
return initvalue;
};
}
addProperty.upperCaseFirstLetter = function(word) {
return word[0].toUpperCase() + word.slice(1);
}
var obj = { };
addProperty(obj, 'test', 10);
console.log(obj.getTest()); // 10
obj.setTest(100);
console.log(obj.getTest()); // 100
console.log(obj); // { getTest:..., setTest: ...} - самого свойства тут нет