У меня есть приложение, которое позволяет пользователям создавать объекты и хранить их (в таблице MySQL, как строки) для последующего использования. Объект может быть:
function Obj() {
this.label = "new object";
}
Obj.prototype.setLabel = function(newLabel) {
this.label = newLabel;
}
Если я использую JSON.stringify для этого объекта, я получу только информацию о Obj.label
(строковый объект будет иметь строку типа {label: "new object"}
. Если я сохраню эту строку и хочу разрешить пользователю загружать объект позже, метод setLabel
будет потерян.
Итак, мой вопрос: как я могу повторно создать объект, чтобы он сохранял свойства, сохраненные благодаря JSON.stringify, но также возвращает разные методы, которые должны принадлежать его прототипу. Как бы Вы это сделали? Я думал о чем-то, "создав пустой объект" и "объединив его с сохраненными свойствами", но я не могу заставить его работать.