DEV Community

Discussion on: JavaScript Frustration: Classes and Class Properties Transform

Collapse
 
nestedsoftware profile image
Nested Software

Hmmm, interesting. That syntax without parentheses or assignment looks really weird. I tried it, but I can't seem to compile this code at all. I am using the presets "env", "react", and "stage-2"...

Collapse
 
nektro profile image
Meghan (she/her)

are you using babel? that's really odd, because it's vanilla ES syntax...

Thread Thread
 
nestedsoftware profile image
Nested Software • Edited

I think maybe you intended to put parentheses after arrowFunction, i.e:

  arrowFunction() { // <--- added parentheses
    console.log('BaseClass arrowFunction called')
  }

In that case arrowFunction is using the normal class method syntax. If that's what you meant, then yeah, the normal syntax puts the methods of the class in the prototype chain whereas using arrow functions with the "class properties" syntax appears not to. In the latter case, each instance will have its own copy of any such functions...

I tried running your code as-is in my browser and it didn't work. I also tried running it in node both as-is and transpiled with babel, and neither worked. Hopefully it isn't me having a stroke or something! :)