Reputation: 16420
Here I want to reset all properties of Data to their original values, how I do that?
var Data = {
prop1: false,
prop2: true,
prop3: null
}
Some code executes & sets Data.prop1 = "abc";
I never instantiated Data, is that a problem?
Upvotes: 8
Views: 27367
Reputation: 46
There is a bug in a response above...
var Data = {
prop1: false,
prop2: true,
prop3: null,
// save initial values
init: function() {
var origValues = {};
for (var prop in this) {
if (this.hasOwnProperty(prop) && prop != "origValues") {
origValues[prop] = this[prop];
}
}
this.origValues = origValues;
},
// restore initial values
reset: function() {
for (var prop in this.origValues) {
this[prop] = this.origValues[prop];
}
}
}
Data.init();
Data.prop1 = true;
Data.prop2 = false;
Data.prop3 = "xxx";
Data.init();
Data.reset();
alert(Data.prop3);
The "init" function allows you to mutate Original state after entering data by reentrancy. To protect against this the "init" function could be transformed by wrapping it in a closure (IIFE expression) , thus securing the original state and preventing mutation of the properties after the 1st call to the "init"...
var Data = {
prop1: false,
prop2: true,
prop3: null,
// save initial values
init : function() { (() =>{
var origValues = {};
for (var prop in this) {
if (this.hasOwnProperty(prop) && prop != "origValues") {
origValues[prop] = this[prop];
}
}
this.origValues = origValues;
})()},
// restore initial values
reset: function() {
for (var prop in this.origValues) {
this[prop] = this.origValues[prop];
}
}
}
Data.init();
Data.prop1 = true;
Data.prop2 = false;
Data.prop3 = "xxx";
Data.reset();
alert(Data.prop3);
Upvotes: 0
Reputation: 2072
A more elegant way of copying default values without losing the reference:
function createData() {
return {
prop1: 1,
prop2: 2,
prop3: [3]
reset: function() {
Object.assign(this, createData())
}
}
}
var data = createData()
data.prop1 = "not default anymore"
data.reset()
console.log(data.prop1) // 1
As mentioned earlier, you still need to remove properties that were added later if this is the case.
Upvotes: 2
Reputation: 707416
Another way to do it would be this:
var Data = {
prop1: false,
prop2: true,
prop3: null,
// save initial values
init: function() {
var origValues = {};
for (var prop in this) {
if (this.hasOwnProperty(prop) && prop != "origValues") {
origValues[prop] = this[prop];
}
}
this.origValues = origValues;
},
// restore initial values
reset: function() {
for (var prop in this.origValues) {
this[prop] = this.origValues[prop];
}
}
}
Then, when you use it, you call Data.init()
in your initialization code to save the current values so they can be restored later with Data.reset()
. This has the advantage that maintenance is easier. When you add or change a variable to your data structure, it is automatically incorporated into the reset function without you having to make any specific changes.
You can see it work here: http://jsfiddle.net/jfriend00/EtWfn/.
Upvotes: 9
Reputation: 150040
The simplest way that avoids having to duplicate all of the default values is to just use a more traditional constructor and instantiate your Data object from there. Then when you need to reset you can just throw away your old object and instantiate a new one.
Note: in the following code I've used the JS convention that functions intended to be used as object constructors start with an uppercase letter, and the variable referencing an instance starts with lowercase.
function Data() {
this.prop1 = false;
this.prop2 = true;
this.prop3 = null;
}
var data = new Data();
data.prop1 = "abc"; // change some properties of data
data.prop3 = "something else";
data = new Data(); // "reset" by getting a new instance
If you don't like having to use "new" you could just write a non-constructor style function that returns an object:
function getData() {
return {
prop1: false,
prop2: true,
prop3: null
};
}
var data = getData();
data.prop1 = "abc";
data = getData(); // reset
If for some reason you need the reset to keep the same actual instance rather than getting a new identical one then you will need to change the properties back one by one as per Frits van Campen's answer (you'd also need to add some code to delete any extra properties that were added along the way).
Upvotes: 4
Reputation: 57729
Data
is a dynamic
object. You could just add a function that will reset all the values:
var Data = {
prop1: false,
prop2: true,
prop3: null,
reset: function () {
this.prop1 = false;
this.prop2 = true;
this.prop3 = null;
}
}
Upvotes: 14
Reputation: 1825
Sounds simple enough. Would this work for you?
// after the Data structure is defined copy it to another variable
var OriginalData = Data;
// When you need to reset your Data to the Original values
Data = OriginalData;
Upvotes: 0
Reputation: 16051
try that:
var Data = {
prop1: false,
prop2: true,
prop3: null
}
var DataOriginal = {
prop1: false,
prop2: true,
prop3: null
}
Data.prop1 = 'abc';
Data = DataOriginal;
Upvotes: 0