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?
clear() method in JavaScript is used for the removal of all the elements from a set and make it empty. No arguments are required to be sent as parameters to the Set. clear() method and it returns an undefined return value. Parameters: This method does not any accept any parameters.
You can do it both ways. I prefer creating the empty object using {} and then adding the needed props but you can make it by defining the props with the initialization of the value: There initialize a property to null . var car = { color: null, seating: null, fuelconsumption: null }; Just like you did.
In order to set object values to null, you need to get all the keys from the object and need to set null. You can use for loop or forEach() loop. node fileName. js.
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;
}
}
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/.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With