1

So I was searching for a way to deep clone an object in javascript and found this solution :

function keepCloning(objectpassed) {
  if (objectpassed === null || typeof objectpassed !== 'object') {
     return objectpassed;
  }
// give temporary-storage the original obj's constructor
var temporary_storage = objectpassed.constructor(); 
  for (var key in objectpassed) {
    temporary_storage[key] = keepCloning(objectpassed[key]);
  }
  return temporary_storage;
}
var employeeDetailsOriginal = {  name: 'Manjula', age: 25, Profession: 'Software Engineer' };
var employeeDetailsDuplicate = (keepCloning(employeeDetailsOriginal));
employeeDetailsOriginal.name = "NameChanged";
console.log(employeeDetailsOriginal);
console.log(employeeDetailsDuplicate);

My question was that shouldn't we be using new with the constructor?

var temporary_storage = new objectpassed.constructor(); 

Then I realised that the object passed is made using object literals{} and have constructor as Object();

I made a first class constructor function Person()

function Person(name, age, profession){
this.name=name;
this.age=age;
this.profession=profession;
}
var employeeDetailsOriginal = new Person('Manjula', 25,'Software Engineer');
var employeeDetailsDuplicate (keepCloning(employeeDetailsOriginal));

Now when I used the keepCloning method, it threw an error that temporary_storage is undefined meaning that

objectpassed.constructor();

must have returned undefined!

So I want to know that do we or do we not use the new keyword before constructor?

I googled it but didn't found any satisfactory explanation!

2) In the same context of the question

function A(){

}

var a =  new A(); //1

var a1= A.prototype.constructor(); //2

var a1 = new A.prototype.constructor(); //3

Which of the (2) and (3) is the exactly similar method to (1) for constructing an object of A?

5
  • Someone else will answer your question about the right way to call the constructor since I am not positive I will give the correct answer, but when I need to deep copy an object I just do var newobject = JSON.parse(JSON.stringify(oldobject)) Does that work for your use case? Commented Dec 2, 2017 at 17:16
  • The Json method does work but I had a doubt regarding if new is used with constructor or not? Commented Dec 2, 2017 at 17:18
  • Yes. That is a good question that I will leave to someone smarter than me. But wanted to be sure you were aware of the JSON.parse shortcut :) Commented Dec 2, 2017 at 17:22
  • cloned = Object.assign({}, original); - why so complicated? Commented Dec 2, 2017 at 17:26
  • @Jonas W. Thank you but I just want to know how to use new keyword here? The complication is due to the explanation of the question! This method works too! Just wanted to increase my JS knowledge! Commented Dec 2, 2017 at 17:30

1 Answer 1

2
 new constructor();

is equal to

(function(){
  const obj = Object.create(constructor.prototype);
  constructor.call(obj);
  return obj;
})()

So with new it returns a new object, without it just calls the constructor function, and as it returns nothing its undefined. So yes its mandatory.

new A() === new A.prototype.constructor();
A() === A.prototype.constructor();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.