1
I have a class, example:
class Animal {
constructor(raca) {
this._raca = raca;
}
get raca() {
return this._raca;
}
printRaca = function() {
console.log(this._raca);
}
}
And then I extend it this way:
class Cachorro extends Animal {
}
Notice that I did not call the builder, nor the super()
. However, if I instantiate the class Cachorro
it works the same way as class Animal
, and I can take the this._raca
even without having called the constructor(raca) { super(raca) }
and use it normally, see:
class Animal {
constructor(raca) {
this._raca = raca;
}
get raca() {
return this._raca;
}
printRaca = function() {
console.log(this._raca);
}
}
class Cachorro extends Animal {
}
var dog = new Cachorro('poodle');
console.log(dog.raca); //'poodle'
dog.printRaca(); // 'poodle'
What is the need then to call the builder and the super()
? Is there any consequence in not calling them?
I noticed it’s a mistake.
– Maniero
Where? I did here several times and I had no error, including running the snippet right here on the site works.
– Máttheus Spoo
SyntaxError: fields are not currently supported
– Maniero
Can you at least tell me why it works for me and not for you? I have nothing special in my browser, I am using the normal Chrome in a Ubuntu 18.0
– Máttheus Spoo
Google Chrome works, Firefox does not, giving error "Syntaxerror: Fields are not Currently supported"
– Woss
The problem of incompatibility is in doing
printRaca = function...
instead of declaring it as a normal class method,printRaca() {...}
.– Woss
https://stackoverflow.com/questions/56083707/how-to-fix-syntaxerror-fields-are-not-currently-supported-error-in-javascript https://www.chromestatus.com/feature/6001727933251584
– DaviAragao