I need a way to assign functions to object that then will be stored in the localstorage. I tryed the code below, but I get: "Uncaught TypeError: mydog.woof is not a function"
Storage.prototype.setObject = function(key, value) {
this.setItem(key, JSON.stringify(value));
}
Storage.prototype.getObject = function(key) {
var value = this.getItem(key);
return value && JSON.parse(value);
}
var Dog = function(name) {
this.name = name
}
Dog.prototype.woof = function woof() {
console.log('woof woof, I am ' + this.name);
}
// Instantinate your objects with `new` keyword
var dog = new Dog('Lovely')
dog.woof()
//Store in the localstorage
localStorage.setObject('mydog', dog);
//Get from the localstorage
var mydog = localStorage.getObject('mydog');
mydog.woof();