I'm fairly new to JS classes, and am doing mostly back-end work.
I was playing around with the new JS classes and so I started going through the examples here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
I went to the chrome (chromium) developer tools console and I wrote the Polygon class:
class Polygon {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
Then I wanted to redefine the class, according to the example containing the methods, so I wrote:
class Polygon {
constructor(height, width) {
this.height = height;
this.width = width;
}
get area() {
return this.calcArea();
}
calcArea() {
return this.height * this.width;
}
}
This raises an error: Uncaught SyntaxError: Identifier 'Polygon' has already been declared(…)
Now I understand there's a new scoping in ES6, and that classes automatically use the new scoping and so on... but really, how do I redefine my class? :D
I'm writing Python usually, so I'm used being able to redefine everything I want.
Polygon
but one defined with a third property and method?var Polygon = <my class>; Polygon = <redefine the class>
. I'm trying to redefine the class, which means I want to forget the old definition, and use the new one.Polygon = class { ... }
to reassign the class.class Foo {}
is exactly likelet Foo = class Foo {};
so you can reassignFoo
if you want, but you can't do a second declaration because you can'tlet
-declare the same thing twice.