In Javascript, how can i store the reference of a value, rather than copying the value?
Suppose i created a window popup using const popup = window.popup(url, config)
and that if i want to check if that popup is closed, i can just console.log popup.closed
.
This would return a boolean value depending on the state of the popup. All Cool.
Now, in vuejs 3, I want to look for changes in this value to trigger a function.
What i did is, I created a data property isClosed
and assigned value as false
(becasue after opening a popup, & checking, closed would be false
) like below:
data() {
return {
isClosed: false
}
}
Now after creating a popup, i assigned this isClosed
to popup.closed
:
this.isClosed = popup.closed
Now what I was doing is, I added a watcher property to trigger a function when the value is changed:
watch: {
isClosed() {
console.log("value changed");
// the function to trigger here
}
}
Now the thing is, its not even logging when i close that popup. Then i realised its just copying the value of popup.closed rather making a ref.
So, how can i possibly do this without setInterval
?
What I'm doing now is, I'm reassigning that isClosed
data property every 3sec so that it would change the value of isClosed
& trigger the watcher & thus trigger the function.
const timer = setInterval(() => {
this.isClosed = popup.closed;
if (this.isClosed) {
clearInterval(timer)
}
}, 3000)
But the big blow on my face is, why use a watcher when we can just add the function to trigger inside this timer, to trigger when the value of popup.closed
is true.
So, how can i store the reference of a the popup.value, rather than copying the value?