Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset this JS object?

Tags:

javascript

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?

like image 476
williamsandonz Avatar asked Dec 09 '11 02:12

williamsandonz


People also ask

How do you clear something in JavaScript?

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.

How do you make an object empty?

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.

How do you nullify an object in JavaScript?

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.


3 Answers

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;
    }
}
like image 127
Halcyon Avatar answered Oct 14 '22 03:10

Halcyon


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/.

like image 27
jfriend00 Avatar answered Oct 14 '22 05:10

jfriend00


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).

like image 4
nnnnnn Avatar answered Oct 14 '22 03:10

nnnnnn