1

I am using jQuery and I am still pretty new to JavaScript. I am implementing an object as the following:

MyObject = {
  properties     : [{}],
  resetProperties: function resetProperties() { this.properties = [{}] }
};

As you can see in the above code I can reset the properties by running MyObject.resetProperties() but, in order to do that, I state two times the [{}] variable. How should I accomplish the same thing without repeating that code?


Update

I tried to do the following:

MyObject = {
  properties       : this.propertiesDefault,
  resetProperties  : function resetProperties() { this.properties = [{}] },
  propertiesDefault: [{}]
};

but I get "TypeError: invalid 'in' operand MyObject.properties" and I am not sure that is the right way to proceed.

hippietrail
  • 15,848
  • 18
  • 99
  • 158
user12882
  • 4,702
  • 9
  • 39
  • 54

3 Answers3

1

It seems to me that it would be impossible to avoid having your default / reset properties as a separate object to the one that will be modified.

I would recommend having a default value, and cloning it in your initialisation and reset function. Since you tagged your question with jQuery, I assume you are happy to clone the object with that:

MyObject = {
    defaultProperties : [{}],
    properties : jQuery.extend(true, {}, this.defaultProperties),
    resetProperties: function() { 
        this.properties = jQuery.extend(true, {}, this.defaultProperties);
    }
};

See this Stack Overflow question for more information on cloning objects:

What is the most efficient way to deep clone an object in JavaScript?

This is the documentation for jQuery.extend:

http://docs.jquery.com/Utilities/jQuery.extend

Community
  • 1
  • 1
null
  • 1,187
  • 1
  • 8
  • 20
  • Actually, I don't think this method of cloning ( ie with jQuery ) will work. However some other deep clone method should work for you. Perhaps something like: JSON.parse( JSON.stringify( this.defaultProperties ) ); – null Nov 23 '12 at 19:04
0

From what I know this isn't possible. You're going to have to hard-code the property reset. I tried setting a variable cache outside the object, but when I reset the property it unfortunately maintains its value.

 var obj = {
     p: [ {} ],
     r: function() { this.p = this.cache; }
 };

 obj.cache = obj.p; // attempt to set to original

 obj.p[0].m = 5; // modify

 obj.r(); // reset

 --------

 >>> obj.p[0].m; // 5

We can assume the the cache property is being modified in the same way as p is. Therefore, we can't reset like that.

David G
  • 94,763
  • 41
  • 167
  • 253
0

Depends on what you want. Since you're new to javascript, you may be unfamiliar with using functions to create custom objects, which is the general javascript "OOP" kinda way to do it.

function MyObjectClass() {
    this.properties = null;
    this.resetProperties();
}
MyObjectClass.prototype.resetProperties = function () { this.properties = [{}] };

var MyObject= new MyObjectClass();

But we don't really know that function MyObject needs to fulfill. There may be a requirement that it NEEDs to be a plain old javascript object. Or maybe not, and you're done.

Of course, you can always directly:

MyObject = {
          properties     : null,
          resetProperties: function () { this.properties = [{}];}
        };
MyObject.resetProperties();
JayC
  • 7,053
  • 2
  • 25
  • 41