I was testing with the basic with the below code
var A={};
var B={name:"come"};
A.pro=B;
B=null;
//Here A.pro is still point to normal B, not to a null
How should I do this if I want when B changes, A.pro will also change?
You can use an anonymous function, like :
var A = {};
var B = {
name: "come"
};
A.pro = function() {
return B
};
// or with arrow function
// A.pro = () => B;
B = null;
console.log(A.pro());
If you want to update B
value with A.pro()
, you can use an optional parameter, like :
var A = {};
var B = {
name: "come"
};
A.pro = function(newVal) {
(typeof newVal === 'undefined') ? false : (B = newVal);
return B;
};
B = null;
console.log(A.pro());
A.pro(4);
console.log(A.pro());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With