Can I preserve a copy of an object, inside that object when its created
This is my function which 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 the object when it is initialized inside that 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 its giving me a infinite loop of copies of current object in baseCopy; ANd also it is automatically updating when ever I update any attributes of my object.
1. How is this possible such that, I can preserve a copy of an object with the initial values, inside that object when its created?
Since you’re already using jQuery the generic answer is:
will produce a “deep” copy of any property values that exist at the time, without it recursively referring to itself.
NOTE – I did include some generic JS code, but then the OP completely changed the question, so it’s simpler to revert back to just using jQuery. This will of course copy any methods that exist directly on the object, although if written properly these methods would be on the object’s prototype.