Can I save a copy of an object inside this object when it is created

Is it possible to save a copy of an object inside this object when its created

This is my function that creates a new student object:

function student(id, name, marks){ this.id = id; this.name = name; this.marks = marks; } 

I would like to create a copy of an object when it is initialized inside this object: I came up with this:

 function student(id, name, marks){ this.id = id; this.name = name; this.marks = marks; this.baseCopy = this; } 

But the problem is that it gives me an infinite loop of copies of the current object in baseCopy; ANd is also automatically updated when I update any attributes of my object.

1. How is it possible that I can save a copy of the object with the initial values โ€‹โ€‹inside this object when it is created?

+7
source share
3 answers

Since you are already using jQuery, the general answer is:

 this.baseCopy = $.extend(true, {}, this); 

will create a โ€œdeepโ€ copy of any property values โ€‹โ€‹existing at that time without recursively accessing it.

NOTE I included some common JS code, but then the OP completely changed the question, so itโ€™s easier to get back to the simple use of jQuery. This, of course, will copy any methods that exist directly on the object, although if they are correctly written, these methods will be in the prototype of the object.

+7
source

JQuery-less alternative:
(Just to let you know that this can also be done without.)

 function student(id, name, marks){ this.id = id; this.name = name; this.marks = marks; var tempCopy = {}; // Initialize a temporary variable to copy the student to. for(key in this){ // Loop through all properties on this (student) if(this.hasOwnProperty(key)){ // Object.prototype fallback. I personally prefer to keep it in, see Alnitak comment. tempCopy[key] = this[key]; // Copy the property } } this.baseCopy = tempCopy; // "Save" the copy to `this.baseCopy` } var s = new student(1, 'Jack', [5,7]); s.marks = s.marks.concat([6,8]); // Jack gotten 2 new marks. console.log(s.name + " marks were: ", s.baseCopy.marks); console.log(s.name + " marks are: ", s.marks); // Logs: // Jack marks were: [5, 7] // Jack marks are: [5, 7, 6, 8] 

The advantage of this is that it will automatically copy all the studentโ€™s properties without having to manually set them in baseCopy .
Also, since it does not use jQuery, it works a little faster. This can be significant when dealing with large amounts of data.

+2
source

The following method works for me (maybe this is not the best solution, maybe there is another that includes prototypes of objects)

 function BaseStudent(id, name, marks) { this.id = id; this.name = name; this.marks = marks; } function Student(id, name, marks) { this.baseCopy = new BaseStudent(id, name, marks); BaseStudent.call(this, id, name, marks); } var s = new Student(1, 'Name', 100); console.log(s.id); // logs 1 console.log(s.name); // logs 'Name' console.log(s.marks); // logs 100 s.name = 'AnotherName'; console.log(s.name); // logs 'AnotherName' console.log(s.baseCopy.name);โ€‹ // logs 'Name' 
+1
source

All Articles