I'm having trouble understanding when a data assignment to an object is a reference and when a copy of the object is created. I thought I understood but the following example doesn't fit my simple undestanding of it.
An event handler starts off a series of steps as follows.
Promise.allSettled( [ gather_write, get_build ] ).then( test );
Promises gather_write
and get_build
perform unrelated functions. The first gathers some data from the DOM and writes it to the database. The second retrieves data from the database and builds a document fragment. If both fulfill, then a node in the DOM is replaced with the fragment.
It's too much code to show here, but get_build
, after successfully getting the data from the database, invokes a separate function that builds the document fragment, and was returning its result as a property of the resolve object. That worked fine; and then I wanted to try to provide options to the user in the event that gather_write
rejected and get_build
fulfilled, which requires the temporary storing of the document fragment. So, instead of returning it and passing it back to the Promise.allSettled
, it is stored in a property of the function that builds it.
In the synchronous function build
the code is set up as:
function build()
{
let f;
try
{
f = document.createFragment();
// ...build the fragment...
build.html = f;
}
catch(e)
{ }
finally
{ f = null; }
} // close build
Function build
has to complete before the promise get_build
can resolve, after which the Promise.allSettled
can be evaluated; and, if both promises fulfill, the function to replace the DOM node with the newly built fragment stored in build.html
can be invoked. I thought that build.html
would be a reference to the node object f
and that, since f
is set to null in the finally
block, that would take place before all the above could complete, and when the code to use build.html
was finally run it would be pointing to a null rather than the fragment. So, the assignment statement should be build.html = f.cloneNode(true)
.
However, the process works fine with and without using f.cloneNode
. Would you please explain why? I don't want the browser to take the steps to clone the fragment if it is unnecessary, but hesitate to exclude it without understanding why it is working without cloning.
Thank you.