0

I have the following code :


var f1 = document.getElementById("f"), 
f1 = function () {
var rd1 = new FileReader();
rd1.onload = function () {
              
             function assign () { 
                toto = function toto() {
                     // Code there
             }


        rd1.readAsBinaryString(f1.files[0]);
    }; 
f1.addEventListener('change', rd1);

What I want is to be able to call the function toto() outside of this above block. Is there a way to do that or the scope doesn't allow it ?

Abtchadou
  • 3
  • 2

1 Answers1

1

Function declarations are scoped to the function they are defined in.

To access them outside, you need to explicitly assign them somewhere.

For example:

let toto = null;

function assign() {
     toto = function toto () { ... };
}

… however, scope aside, you are trying to call the function before it has been created as per the classic asynchronous problem so you'll need to rethink your approach.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335