Questions tagged [class-fields]

55 questions
15
votes
3 answers

What are "class fields" in JavaScript?

I was reading about JavaScript classes, and came across this term "public class fields syntax". On digging a bit deeper into it I came across this Babel's documentation on class properties. Can someone please explain - implementation-wise what are…
Aaditya Sharma
  • 3,330
  • 3
  • 24
  • 36
15
votes
2 answers

What is the difference between class fields and properties in Javascript

I'm reading class fields proposal for JavaScript. I don't understand why the authors call it 'fields' and not properties. MDN docs in class article speak about instance properties declared inside constructor and in next section about field…
bifi
  • 175
  • 1
  • 7
14
votes
1 answer

Arrow vs classic method in ES6 class

Is there any reason to write classic syntax of ES6 methods? class MyClass { myMethod() { this.myVariable++; } } When I use myMethod() as callback on some event, I must write something like this (in JSX): // Anonymous…
Vesmy
  • 1,190
  • 4
  • 15
  • 29
13
votes
1 answer

How to use private class fields in nodejs 12?

In the current release of nodejs i.e. 12.x.x, we can declare private class fields by the #some_varible notation. The # notation would make the variable private field for that particular class. class Foo { #some_varible = 10; } I have the…
Rajan Sharma
  • 2,211
  • 3
  • 21
  • 33
11
votes
6 answers

js dynamically access private fields (properties/members)

I'm trying out the new class private member feature However, I've quickly run into a problem: How does one dynamically access them? I expected it to follow pre-existing syntax of either constructor(prop, val) { this[`#${prop}`] = val; //…
Jakob Jingleheimer
  • 30,952
  • 27
  • 76
  • 126
10
votes
2 answers

How to Enumerate Private JavaScript Class Fields

How do we enumerate through private class fields? class Person { #isFoo = true; #isBar = false; constructor(first, last) { this.firstName = first; this.lastName = last; } enumerateSelf() { console.log(this); // (pub/priv…
anthumchris
  • 8,245
  • 2
  • 28
  • 53
10
votes
3 answers

When using React Is it preferable to use fat arrow functions or bind functions in constructor?

When creating a React class, which is preferable? export default class Foo extends React.Component { constructor (props) { super(props) this.doSomething = this.doSomething.bind(this) } doSomething () { ... } } or export default class…
davegri
  • 2,206
  • 2
  • 26
  • 45
8
votes
1 answer

Why are arrow functions as static members values not lexically scoped?

class Foo { static v = 123; static bar = () => this.v; } console.log(Foo.bar()); I expect this code to return undefined, because arrow functions are lexically scoped, hence this must be eagerly bound to the outer scope. Yet, it returns…
zerkms
  • 249,484
  • 69
  • 436
  • 539
7
votes
1 answer

Access private members from developer console?

Before, I would use the old convention of naming fields intended to be private with an _ suffix or prefix. class X{ constructor() { this.privateField_; } privateMethod_() {} } Now that real private accessibility is possible with the #…
4
votes
3 answers

Private class field in JavaScript

class A { #a = 1; static #a = 2; } Results in Uncaught SyntaxError: redeclaration of private name #a in Firefox Uncaught SyntaxError: Identifier '#a' has already been declared in Chrome While class A { a = 1; static a = 2; } is valid in…
Carter Li
  • 149
  • 12
4
votes
1 answer

Accessing a class field on a superclass

I have a file with the following code: class Animal { doSomething = () => { return 'Hi.'; }; } class Dog extends Animal { doSomething = () => { return super.doSomething() + ' Woof!'; …
Elias Zamaria
  • 96,623
  • 33
  • 114
  • 148
4
votes
3 answers

What's difference between two ways of defining method on React Class in ES6

I see this a lot in ES6 React code class Foo extends React.Component { bar = () => { console.log("bar") } baz() { console.log("baz") } } Seems like they both define methods bar and baz on Foo but how are they different.
4
votes
2 answers

ESLint shows error on class instance property initialized to arrow function

maybe similar to How do I configure ESLint to allow fat arrow class methods When class method defined as arrow function Eslint highlight error 'method' is not defined. (no-undef). simple example class abc { d = () => { // method body …
n06rin
  • 173
  • 2
  • 9
3
votes
2 answers

Cannot read private member from an object whose class did not declare it...?

In this program: class Example { #privateMember = 123; // these are fine addNumber (n) { return this.#privateMember + n; } doAddNumber (n) { return this.addNumber(n); } // "cannot read private member #privateMember from an //…
Jason C
  • 38,729
  • 14
  • 126
  • 182
3
votes
1 answer

Unexpected token "=" reported while running eslint on arrow functions

I have a JavaScript class and inside it I have an async method which looks like below. class ABC { func = async () => { //----line 10 //some code } func2 = () => { //----line 11 //some code } } When I run ESLint…
Naxi
  • 1,504
  • 5
  • 33
  • 72