Javascript: Are objects returned by reference, or is a new instance instantiated?

Suppose we have the following js function that processes Date objects:

targetDate and referenceDate are for Date objects.

 function validateDate(targetDate, referenceDate) { if (targetDate < referenceDate) return referenceDate; else return targetDate; } 

Now suppose we want the returned object to be a new instance, and not a pointer to the same memory address as the input parameters.

To do this, is it necessary to replace return dateObjectInstance with return new Date(dateObjectInstance) , or does return already instantiate a new object?

In other words: Objects returned by reference or instantiated by a new instance?

+4
source share
1 answer

Your original assumption is correct - without using new , a reference to the original object is returned.

And, as PointedEars points out, there is no such thing as return by reference.

+4
source

Source: https://habr.com/ru/post/1485386/


All Articles