0

I have a problem understanding Javascript's engine technique called hoisting.

I've tried to create a var in the top of my file to quickly access and edit, but i want to use a variable that has yet to be declared.

First attempt:

var easy_to_edit_value = "some text " + a_yet_to_be_defined_var;

//imagine loads of code so its hard to find the correct funtion to edit the log

function my_function (a_yet_to_be_defined_var){
    console.log(easy_to_edit_value);
}
my_function("more text");

This generates an error at line 1 since a_yet_to_be_defined_var is not defined.

After looking at this post: post-by-apsillers i tried again but this time declaring the var without value (so its known but undefined untill declared somewhere futheron)

var a_yet_to_be_defined_var; // now its known so this error is gone
var easy_to_edit_value = "some text " + a_yet_to_be_defined_var;

function my_function (a_yet_to_be_defined_var){
    console.log(easy_to_edit_value);
}
my_function("more text");
//still undefined


//new attempt with a fresh var being set in the function before being called
var new_var;
var easy_to_edit_value = "some text " + new_var;

function my_function2 (a_yet_to_be_defined_var2){
  new_var = a_yet_to_be_defined_var2;
    console.log(easy_to_edit_value);
}
my_function2("more text");
//still undefined

But this outputs: some text undefined where i was expecting the some text more text since i filled the var before asking for it.

Please note these functions aren't ran using my_function("something") but are triggered by this: client.on('message', my_function);, i've seen arrow function solutions for related questions but i'm not sure how i would get that to work here.

Is it possible to make this work?

Ramon de Vries
  • 1,312
  • 7
  • 20
  • although `a_yet_to_be_defined_var` is declared later on it's not in the same scope as the attempted use and is only available within the scope of `my_function`. Variable hoisting does not work in this situation. – phuzi Apr 08 '21 at 13:53
  • `new_var` is declared and used before it is given a value - this is a completely different reason for the apparent same result. – phuzi Apr 08 '21 at 13:54

2 Answers2

1

Instead of defining a value called easy_to_edit_value, change it to a function called easy_to_call_function that will return "some text " concatenated with the current value of new_var.

Once a (var or let) variable is assigned, it must be reassigned or reevaluated each time.

let new_var;
const easy_to_call_function = () => "some text " + new_var;

function my_function2(a_yet_to_be_defined_var2) {
  new_var = a_yet_to_be_defined_var2;
  console.log(easy_to_call_function()); // Call the function
}

my_function2("more text");
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
0

You need to re-declare the variable inside of the function:

var new_var;
var easy_to_edit_value = "some text " + new_var;

function my_function2(a_yet_to_be_defined_var2) {
  new_var = a_yet_to_be_defined_var2;
  easy_to_edit_value = "some text " + new_var;
  console.log(easy_to_edit_value);
}
my_function2("more text");

If one variable is changed, other variable will not update according to the new value. That kind of programming requires a really high level compiler, which not many people are willing to make. The programmer must update variables on their own.

Rojo
  • 2,749
  • 1
  • 13
  • 34
  • So this would take editing the `easy_to_edit_value` inside the function to get any effect right? so basicly what i'm trying to achieve is impossible. – Ramon de Vries Apr 08 '21 at 13:34
  • @RamondeVries Yes. I *think* ReactJS has something like that, but I'm not entirely sure. – Rojo Apr 08 '21 at 13:35