I come from a PHP/Ruby background, so I'm still trying to wrap my head around the strange logic that JavaScript uses for its version of OOP.
My question is, why can't I declare a variable within the scope of an ES6 class, without it having to be inside of a method, or outside the class as in a global variable?
// The below works
// var example = [];
class Example {
var example = []; // SyntaxError: Unexpected identifier
constructor(value) {
this.value = value;
example.push(this.value);
}
get values() {
return example;
}
}
list = new Example('shoes');
console.log(list.values);
I feel like that's a serious limitation for privacy, are there any hacks that allow what I'm asking for to be possible within its class scope? Or is there something else that I'm missing?