0

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?

  • What if you specify `example : [] ` ? – pixlboy Aug 30 '18 at 15:54
  • @rach8garg That’s not valid syntax in a class. – Sebastian Simon Aug 30 '18 at 15:54
  • 1
    There’s a [class fields proposal](https://github.com/tc39/proposal-class-fields). Maybe this will be available next year. – Sebastian Simon Aug 30 '18 at 15:55
  • thanks @Xufox. Possible duplicate : https://stackoverflow.com/questions/22528967/es6-class-variable-alternatives – pixlboy Aug 30 '18 at 15:55
  • But why is it not valid, see the link i pasted. – pixlboy Aug 30 '18 at 15:56
  • @rach8garg I don’t see a `:` used anywhere in the ES6 class example on that question. – Sebastian Simon Aug 30 '18 at 15:58
  • Ohh, i see that's a framework class. – pixlboy Aug 30 '18 at 16:06
  • Thanks for the link @rach8garg, that answers my question in a pretty descriptive way. – Juan Gongora Aug 30 '18 at 16:10
  • Do you want that `example` variable to be global (shared by all instances - which is a bad idea for a mutable array) or to be local to each instance? If the latter, just put it inside the constructor body. – Bergi Aug 30 '18 at 17:34
  • @Bergi no I don't want it to be global, I was just demonstrating in that example that it is capable of working as a global variable, yet it I can't define it within a class scope. And I already know that I can have it work if it's defined within the constructor, but that's not a class variable. What I was looking for is a class variable, but it seems that ES6 does not offer that capability without making some ugly alternative hacks. – Juan Gongora Aug 30 '18 at 20:32
  • @JuanGongora What do you mean by "class variable" then? If it's per instance, then you probably are looking for an instance property. – Bergi Aug 30 '18 at 20:52
  • @Bergi I mean a variable that is private to the class. So it can't be manipulated by existing instances of the class (like a static method for example), or by data structures that are outside of the class scope. – Juan Gongora Aug 30 '18 at 21:53
  • Great @JuanGongora, hoping for courtesy upvotes. – pixlboy Aug 31 '18 at 05:20
  • @JuanGongora Privacy is based on scope in JS. So you either put your variable next to the `class`, in the same module (where only code from that module scope can access it), or you put the variable inside the constructor (where it is not static but created per instance, and only code from the constructor function scope can access it). – Bergi Aug 31 '18 at 12:24

0 Answers0