JSFiddle - React, Tailwind, and code Playground
by Michael Prosser
JavaScript
var Declaration = function(obj){
this.obj = obj;
this.getKeys = function(){
var keys = [];
for(var key in this.obj){
if (this.obj.hasOwnProperty(key)) {
keys.push(key);
}
}
return keys;
}
this.onchange = null; // function
this.getKeyIndex = function(key){
var keys = [];
var i = 0;
for(var k in this.obj){
if (this.obj.hasOwnProperty(key)) {
if(k == key){
return i;
}
i++;
}
}
return -1;
}
this.getItem = function(key){
var index = this.getKeyIndex(key);
if(index > -1){
return this.obj[this.getKeys()[index]];
} else {
return null;
}
}
this.setItem = function(key,value){
var index = this.getKeyIndex(key);
if(index > -1){
this.obj[this.getKeys()[index]] = value;
return true;
} else {
return false;
}
}
this.json = function(){
return JSON.stringify(this.obj);
}
}
var game1 = new Declaration({
name: "Donkey Kong",
media: "game",
free: false
});
console.log(game1.getKeys());
console.log(game1.getItem('media'));
console.log(game1.json());
game1.setItem('free',true);
console.log(game1.obj);
console.log(game1.getItem('free'));
console.log(game1.json());