-1

Instead of just assigning/copying the value of var b to var a via a = b;,

I‘d like to make a ‚link‘ to b, so that when a is changed, actually b is changed and when a is used, actually b is used, so to say. Is that possible?

(You could call it a reference).

Thanks in advance.

  • 3
    Well that is not really possible with regular variables. Could do something with objects. If we see some actual code we might be able to suggest a better solution. – epascarello Sep 13 '22 at 12:21
  • 2
    Does this answer your question? [Pointers in JavaScript?](https://stackoverflow.com/questions/10231868/pointers-in-javascript) – Michael M. Sep 13 '22 at 12:21
  • [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) objects ? – Gabriele Petrioli Sep 13 '22 at 12:23
  • 2
    Please visit the [help], take the [tour] to see what and [ask] since we are "LovingGreatQuestions" – mplungjan Sep 13 '22 at 12:24
  • @GabrielePetrioli That sounds like it, thank you, i will look into it. You may consider posting this as an actual answer. – LovingGreatAnswers Sep 13 '22 at 12:28
  • That's a very bad idea. You want to divert an established and recognized functioning on a specificity of this language. Sorry, but this is something that really pisses me off. think about code maintenance, think about those who will have to reread this kind of code and whom you will trap with this kind of feature, think about the bugs that this can cause and the possible disasters that can occur during execution,... – Mister Jojo Sep 13 '22 at 12:34

1 Answers1

2

What's wrong with using objects?

const a = {
  current: 5,
}

const b = a

b.current = 10

console.log(a, b)
Konrad
  • 21,590
  • 4
  • 28
  • 64