-1

I am creating a normal ES6 class. The problem encountered is putting a variable inside the class encapsulation while keeping it outside of constructor().

You can replicate this error when you use this code:

class Polygon {  
     constructor() {
          this.name = "Polygon";

          var goodboy = "I like pie...I can stay since I'm legal!";
     }

     var badboy = "what're you gonna do when they come for you? delete me to get rid of all your problems!";

}

var poly1 = new Polygon();

console.log(poly1.name);

You can try this code anywhere! Try deleting badboy and goodboy and see how it plays out.

How can I get around this?

Any explanation is welcome, Farouk

Aforementioned code was modified from MDN. This code has no intention and is meant for variable declaration example purposes.

faroukcharkas
  • 223
  • 3
  • 5
  • What are you trying to do? There is no such thing as a "*`class` encapsulation*" in which you could put arbitrary statements. It's not a block, it's a `class` body and can only contain method definitions. – Bergi May 11 '18 at 21:51
  • 1
    Enter the confusion caused by TypeScript (making JS look like Java). – Randy Casburn May 11 '18 at 21:56
  • What your trying to do does not really exist in Javascript yet, but it looks like it's on the way -> https://github.com/tc39/proposal-class-fields – Keith May 11 '18 at 21:57

4 Answers4

1

You can declare class level variables like so

class Polygon {
    myVar = null;
    constructor(args){
       this.myVar = args.myVar;
    }
}
let polygon = new Polygon({myVar:"some value"});
console.log(polygon.myVar); // "some value"

To accomplish this by transpiling with Babel you will need to use transform-class-properties plugin.

stemsmit
  • 31
  • 6
0

Cannot define variables outside of a class's constructor function

Yes, that's not syntactically valid.

I'm putting a variable inside the class encapsulation while keeping it outside of constructor().

That's not possible. A class does not provide any such encapsulation, it does not create a block scope. Just put the variable outside of the class definition. Or don't make it a variable but a property instead.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0
// adjusted
   class Polygon
   {
      constructor()
      {
         this.name = "Polygon";

        var goodboy = "I like pie...I can stay since I'm legal!";
      }
   }

   var poly = new Polygon();

   console.log(poly.name);

   // option 2 
   // This works also

   function Polygon2()
   {
      this.name = "Polygon";

      class Poly
      {
        constructor(obj)
        {
            this.user = "Farouk";
            this.name2 = obj.name;
        }
      }

      let poly2 = new Poly(this);

      this.user = poly2.user;
      this.name2 = poly2.name2;
   }

   var poly2 = new Polygon2();

   console.log(poly2.name);
   console.log(poly2.user);
   console.log(poly2.name2);
Ifeanyi Amadi
  • 776
  • 5
  • 10
0

I think, inside a class you can not declare a member directly, you need to use getter and setter. Otherwise you will get an error: "Unexpected token. A constructor, method, accessor, or property was expected."

Here is how you can get your badboy:

class Polygon {

  constructor() {
    this.name = "Polygon";
  }

  get badboy(){
    return  "what're you gonna do when they come for you?";
  }
}

var poly1 = new Polygon();
console.log(poly1.name); // Polygon
console.log(poly1.badboy); // what're you gonna do when they come for you?

Intention of your code is not clear enough. If you will not use it as an instance property, why would you put it in a class?

You know poly1.badboy = "what're you gonna do when they come for you?" does the same thing without all the hassle.

snnsnn
  • 10,486
  • 4
  • 39
  • 44