Questions tagged [class-fields]
55 questions
3
votes
1 answer
Can I set a private class field using a variable as identifier? How?
Node.js 12 supports private class fields denoted by # out-of-the-box, without flags or transpilers.
For example, this works with Node.js 12:
class Foo {
#bar = 1;
constructor({ bar }) {
this.#bar = bar;
}
get bar() {
return…

Patrick Hund
- 19,163
- 11
- 66
- 95
3
votes
4 answers
In a React Component, what's the difference between foo(){} and bar = () => {} and when should I use which?
Babel is doing its magic, which makes me very confused about what's going on.
What's the difference between foo and bar in this react Component? And when should I use which?
class MyComponent extends Component {
foo() {
//...
}
bar = ()…

ZYinMD
- 3,602
- 1
- 23
- 32
2
votes
1 answer
Why do extended classes uses prototypes for methods but not for fields?
Looking at this simple code :
class Animal {
someField = 42;
animalFunc() {
console.log('animal')
}
}
class Lion extends Animal {
lionFunc() {
console.loge('lion')
}
}
let lion = new…

Royi Namir
- 144,742
- 138
- 468
- 792
2
votes
2 answers
Why does ESLint not recognize my class arrow functions?
I followed the advice on How do I configure ESLint to allow fat arrow class methods which states to set the parser to babel-eslint.
I installed it and updated my config file as follows:
{
"parserOptions": {
"parser": "babel-eslint",
…

user17791008
- 247
- 2
- 12
2
votes
0 answers
ESLint in React: Parsing error: Unexpected token =
I'm getting this error:
error Generating development JavaScript bundle failed 92:7 error
Parsing error: Unexpected token =
I am using gatsby & a custom eslintrc:
{
"parser": "babel-eslint", // uses babel-eslint transforms
…

antonwilhelm
- 5,768
- 4
- 19
- 45
2
votes
1 answer
Is there any way to reflect public instance class fields from the javascript class declaration?
The ecmascript candidate spec allows to declare class fields like:
class A {
foo;
}
or with value assignment like:
class A {
foo = 'abc';
}
Public instance fields spec on MDN
Is there any way to reflect the list of declared fields names…

majo
- 381
- 3
- 8
2
votes
2 answers
How to get access to private field via square brackets in JavaScript
This code works:
class Test {
#field
get field() {
return this.#field;
}
}
But if I want to calculate field name I have to use square brackets but it doesn't work:
class Test {
#field;
get field() {
return this['#field'];
…

Gosha Egorian
- 51
- 4
2
votes
2 answers
Accessing protected fields of base class from derived (ES2019 private class)
I'd like to access private fields of base class from derived classes without making them public (what is called 'protected' in other languages).
Consider the following class:
class Animal {
#privateProp;
constructor() {
this.#privateProp =…

Tamir Nakar
- 933
- 1
- 10
- 17
2
votes
2 answers
JS Class Fields
With ES6 classes, we have getter and setter properties, but no field option (or at least that I know of).
With Object.defineProperty, you can set them with the value property. If at all, how do you do the same with classes?
I'm aware it can be done…

Spedwards
- 4,167
- 16
- 49
- 106
1
vote
1 answer
Clone private fields of class to implement immutable pattern
I'm trying to use JS classes with private fields for a React app (because I still find it weird to use naked Object instances everywhere). React uses the concept of immutable state, so I have to clone my objects in order to change them. I'm using…

Radu C
- 1,340
- 13
- 31
1
vote
1 answer
Can a child class overwrite a private field inherited from a superclass?
I'm playing around with ES6 classes in JavaScript and I'm wondering if it's possible for a child class to inherit private properties/methods from a superclass, while allowing the subclass to mutate this private property without using any "set"…

WillWillington
- 150
- 8
1
vote
3 answers
Private fields in Javascript don't show up in JSON.stringify
So if I write a class as follows
class Rectangle {
#width;
#height;
constructor() {
this.#width = 3;
this.#height = 5;
}
}
let rect = new Rectangle();
console.log(JSON.stringify(rect)); // returns {}
It will return an…

Joshua Clark
- 21
- 4
1
vote
1 answer
How to get acornjs to properly style check private class fields and methods?
I'm working on a project where I need to use a style check for my code. I want to use acorn js, however it fails when trying to parse private class fields and class methods.
I've tried:
const { Parser } = require('acorn');
const privateMethods =…

Alex
- 131
- 2
- 13
1
vote
1 answer
JS Class: Difference between ES6 myFunc(){..} and ES5 myFunc = function() {...} in a class declaration
In the following code,
class PersonClass {
constructor(fname) {
this.fname = fname;
}
read = function() { console.log('I am reading') }
speak () { console.log('I am speaking'); }
}
//Instantiate
let p1 = new PersonClass('Raj')
read…

appu
- 471
- 1
- 8
- 26
1
vote
1 answer
Class fields from properties in ReactJS
I am having trouble figuring out how to correctly create a class field based on a property.
class Example extends Component {
example_titles = props.titles;
// ...
}
which results in
Line 7: 'props' is not defined no-undef
I call this…

Min
- 327
- 1
- 4
- 22