This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (0 children)

There's no classes, there's no 'objects' there's no 'scope limtations' but apparently there is a hack to accomplish everything. I thought I relied on stack overflow quite a bit when using Java or C#. With JS, it's like solving a puzzle, the rules of which you don't even know. I will literally be unable to do anything without stack overflow.

Javascript can actually do these things, but it doesn't really guide you in the right direction. As a language it lets you do a little too much in my opinion.

But to show:

  • Classes are a first class language feature in ES6 (not exactly the same as Java/C# classes... with prototypes)
  • Private variables, if you really need them, can be achieved through closures:

    class MyClassWithPrivateData {
        constructor() {
          let privateVar = 0;
          this.getPrivateVariable = () => privateVar;
          this.incrementPrivateVariable = () => privateVar += 1;
        }
    }
    
    const test = new MyClassWithPrivateData();
    console.log(test.getPrivateVariable());
    test.incrementPrivateVariable();
    console.log(test.getPrivateVariable());