0

Let's say I have a variable

let foo = {};

And I'm re-assigning it to something else.

foo = {}

The question is how do I know that the variable is re-assigned ?

Like I've heard about Proxy, but it doesn't work

// Creating a variable
let foo = {}

// Setting up a proxy
const fooProxy = new Proxy(foo, _ => {
  get: function(...) { ... },
  set: function(...) { ... }
}

// I just re-assigned the variable, and I want that `notify()` to tell me about it.
foo = {}

// I wanna call this function when the variable changes,  but it doesn't work 
function notify(){
  console.log("Dude, your variable has changed!");
user15328189
  • 55
  • 2
  • 8
  • 2
    Proxies work on objects, not variables. – Barmar Mar 04 '21 at 09:17
  • 1
    I don't think there's a way to intercept variable assignment. You can use a proxy to catch modifications to the object. – Barmar Mar 04 '21 at 09:17
  • It's not possible to get notified when a variable changes. This seems like [an XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). If you explain what you're actual problem is that you try to solve via this we can probably give you an alternative solution. – VLAZ Mar 04 '21 at 09:18
  • Does this answer your question? [Listening for variable changes in JavaScript](https://stackoverflow.com/questions/1759987/listening-for-variable-changes-in-javascript) – jonrsharpe Mar 04 '21 at 09:19
  • @jonrsharpe. No, it does not. I don't want to create a whole object with properties to change it. – user15328189 Mar 04 '21 at 09:23
  • Just do what mutation observers do but differently, they can monitor elements and changing document states why? Should help! A tip, it can deal with class introductory or lazily with Timeouts. I try to not use timers here for efficiency but if it's necessary you do it. Classes/Functions with callback functions on their change anytime you edit them. This does come down to what you want to do and/or trying to secure with this "check-up" – BGPHiJACK Mar 04 '21 at 09:30
  • @blanknamefornow Can you show an example of how I can implement that ? – user15328189 Mar 04 '21 at 10:03

1 Answers1

-1

There's a few examples but I guess it comes down to purpose? If means are to secure variables from changing, that likely won't happen but you can get as close with call-backs and promises/etc.

class User {
  constructor(name) {
    this.name = name;
    this.skill = "novice";
  }
  setSkill(newSkill) {
    this.skill = newSkill;
    this.alertChange()
  }
  alertChange(){
    console.log(this.name+ " OH NO, My Skill!!!! No SECURITY HERE I GUESS?");
  }
}
let Test = new User("Donny");


Test.setSkill("Egg Champion");

let test = undefined;
let test2 = undefined;
let watchvalue = 0;
function RepeatTillReady() {
  clearTimeout(test);
  clearTimeout(test2);

  test = setTimeout(()=>{
    if (watchvalue == 100) {
       console.log("That took a while but we made it!");
    } else {
      RepeatTillReady();
    }
  }, 200);
  
  test2 = setTimeout(()=>{
   watchvalue++;
  console.log("counting slowly now"+watchvalue);
  }, 100);
}
RepeatTillReady();

Here's another fun example when trying to catch DOM load in browsers! :)

BGPHiJACK
  • 1,277
  • 1
  • 8
  • 16