Can I save a copy of the object, inside this object when I create it - Continue:

This is a continuation. My old question.

This is my function that creates a new student object:

function student(id, name, marks, mob, home){ this.id = id; this.name = name; this.marks = marks; this.contacts = {}; this.contacts.mob = mob; this.contacts.home = home; this.toContactDetailsString = function(){ return this.name +':'+ this.mob +', '+ this.home } } 

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, mob, home){ this.id = id; this.name = name; this.marks = marks; this.contacts = {}; this.contacts.mob = mob; this.contacts.home = home; this.toContactDetailsString = function(){ return this.name +':'+ this.mob +', '+ this.home } this.baseCopy = this; //Not sure about this part } 

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 an object with initial values ​​inside this object when it is created?

2. Is it possible to copy functions

3. I am very curious to find out if this is possible without hard coding attribute names and using pure JS

+6
source share
4 answers

To a large extent, like my answer to your previous question, you can use this code to make a copy of the object and its attached properties, rather than copying it:

 function student(id, name, marks, mob, home){ this.id = id; this.name = name; this.marks = marks; this.contacts = {}; this.contacts.mob = mob; this.contacts.home = home; this.toContactDetailsString = function(){ return this.name +':'+ this.mob +', '+ this.home } // Copy the object to baseCopy this.baseCopy = clone(this); // "clone" `this.baseCopy` } function clone(obj){ if(obj == null || typeof(obj) != 'object'){ // If the current parameter is not a object (So has no properties), return it; return obj; } var temp = {}; for(var key in obj){ // Loop through all properties on a object if(obj.hasOwnProperty(key) && !(obj[key] instanceof Function)){ // Object.prototype fallback. Also, don't copy the property if it a function. temp[key] = clone(obj[key]); } } return temp; } var s = new student(1, 'Jack', [5,7], 1234567890, 0987654321); s.marks = s.marks.concat([6,8]); // Jack gotten 2 new marks. console.log(s.name + " marks were: ", s.baseCopy.marks); // Jack marks were: [5, 7] console.log(s.name + " marks are: ", s.marks); // Jack marks are: [5, 7, 6, 8] console.log(s.baseCopy.toContactDetailsString); // check if the function was copied. // undefined console.log(s.baseCopy.contacts.mob); // 1234567890 

(I will work on a deep copy for a second)

A deep copy should work now.

+3
source

you don't create a copy by saying

this.baseCopy = this; , you just set the link to this internal variable. So baseCopy also points to the same object

You need to create a method that returns a new student object from the transferred student object and then saves it as BaseCopy

+3
source
 this.baseCopy = new student(id, name, marks); 

Your path just makes a circular link. Use new to create a new object.

Using this is likely to go into infinite recursion.

You can get around this as follows:

 function student(id, name, marks, flag) { // usual code... // And: if (!flag) { this.baseCopy = new student(id, name, marks, true); } } 

Thus, only the top student has baseCopy.

+1
source

Well...

 this.baseCopy = this; 

Basically means that the baseCopy object is the object itself. So:

 var abc = new student(someId, someName, someMarks); 

abc and abc.baseCopy actually point to the same object. What you can do is probably change baseCopy to:

 this.baseCopy = { id: id, name: name, marks: marks, contact: {mob:mob, home: home}} 

In principle, manually create a copy of the inputs to the object. Keep in mind that if any of the inputs are reference types, the copy will still point to the same object as the original.

+1
source

All Articles