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.