Say we have a class in the global scope
class SampleClass {
constructor(a) {
this.a = a;
}
}
After this declaration, the class can be instantiated in the global scope like:
var obj = new SampleClass(a); //works
But the same declaration doesn't work inside a function in the global scope, as in:
function sampleFunc() {
var obj = new SampleClass(a); //doesn't work
//...
}
It throws the error:
Uncaught ReferenceError: SampleClass is not defined
How can I create an object of SampleClass
from inside the function sampleFunc
?
I am confused because SampleClass
is declared in the global scope, so I expected it to be visible inside sampleFunc
.