Variable declaration in a javascript class
Hi,
Can we declare global variables within a javascipt class, outside a function? When I try to declare a variable, it gives error: Syntax Error: Unexpected identifier
Sample code:
class Test {
var abc; //received above error
var abc = "Test"; //received above error
let def; //received above error
function1() {
//some code here
var abcd = "New"; //works fine here
}
Please suggest how I can create variables outside the functions in this scenario. Thanks!
This syntax is not allowed in ES6 JavaScript.
https://stackoverflow.com/questions/22528967/es6-class-variable-alternatives
https://stackoverflow.com/questions/41212099/making-a-es6-variable-global-in-a-class
This is not a TestComplete restriction but a restriction in the JavaScript language itself. To declare a variable within a class, it needs to be a property of the class or, as you did so, scoped within a method in the class. It's all about scoping and variables are not supported in the scope definition of a class.